vue路由--动态路由

前面介绍的路由都是路径和组件一对一映射的

有时候需要多个路径映射到一个组件,这个组件根据参数的不同动态改变,这时候需要用到动态路由

 动态路由这样定义路由路径:

path: '/foo/:id'--可以不叫id,任意命名

以下两种路径都会映射到foo组件

<router-link to="/foo/1">foo1</router-link>
<router-link to="/foo/2">foo2</router-link>

后面跟着的参数(1和2)通过$route.params.id获取

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/foo/1">foo1</router-link>
            <router-link to="/foo/2">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id', component: Foo }
]

// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})

// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router // (缩写)相当于 router: router

}).$mount('#app') 
</script>

</html>

例子中只有foo/1和/foo/2链接,可以在浏览器随意输入id,比如:

xxx/xx.html#/foo/3

想要根据参数动态改变页面内容,在mounted里调用不行,mounted只在组件第一次挂载时才调用,因为我们是挂载同一个组件,所以非第1次就不会调用了

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/foo/1">foo1</router-link>
            <router-link to="/foo/2">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
const Foo = Vue.extend({
    
    template: '<div id="testzy"></div>',
    mounted(){
        alert(this.$route.params.id);
        if (this.$route.params.id == 1) {
          document.getElementById("testzy").innerText = "hehe";
        }else{
          document.getElementById("testzy").innerText = "haha";
        }
    }

});

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id', component: Foo }
]

// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})

// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router // (缩写)相当于 router: router

}).$mount('#app')
</script>

</html>

可以在watch里观察路由路径的改变,注意:watch方法调用在挂载之前,所以第一次点击,testzy这个id不存在

document.getElementById("testzy")

返回的是null

第一次以后点击返回正常

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/foo/1">foo1</router-link>
            <router-link to="/foo/2">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
const Foo = Vue.extend({

    template: '<div id="testzy">{{ $route.params.id }}</div>',
    // methods: {
    //     change() {
    //         alert("fd");
    //         // if (this.$route.params.id == 1) {
    //         //     document.getElementById("testzy").innerText = "hehe";
    //         // } else {
    //         //     document.getElementById("testzy").innerText = "haha";
    //         // }
    //     },
    // }
});

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id', component: Foo }
]

// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})



// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router
    watch: {
        $route(to, from) {
            alert(to.path);
            document.getElementById("testzy").innerText = this.$route.params.id;
        }
    },

}).$mount('#app')
</script>

</html>

也可以把watch写在extend里,这个时候,第一次点击(初次挂载)并不会调用watch,因为第一次不算改变,所以得配合mounted一起使用(mounted在第一次挂载调用):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/foo/1">foo1</router-link>
            <router-link to="/foo/2">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div></div>' }
const Foo = Vue.extend({

    template: '<div id="testzy"></div>',
    methods: {
        change() {
          alert("come");
          document.getElementById("testzy").innerText = this.$route.params.id;
            //alert("fd");
            // if (this.$route.params.id == 1) {
            //     document.getElementById("testzy").innerText = "hehe";
            // } else {
            //     document.getElementById("testzy").innerText = "haha";
            // }
        },
    },
    mounted(){
      document.getElementById("testzy").innerText = this.$route.params.id;
    },
    watch:{$route:"change"}
});

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id', component: Foo }
]

// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})



// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router

}).$mount('#app')
</script>

</html>

可以定义多个参数:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/foo/1/11">foo1</router-link>
            <router-link to="/foo/2/22">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
const Foo = Vue.extend({

    template: '<div id="testzy"></div>',
    methods: {
        change() {
          //alert("come");
          document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
            //alert("fd");
            // if (this.$route.params.id == 1) {
            //     document.getElementById("testzy").innerText = "hehe";
            // } else {
            //     document.getElementById("testzy").innerText = "haha";
            // }
        },
    },
    mounted(){
      document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
    },
    watch:{$route:"change"}
});

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id1/:id2', component: Foo }
]

// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})



// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router

}).$mount('#app')
</script>

</html>
原文地址:https://www.cnblogs.com/cowboybusy/p/9505771.html