·这里通过路由的props设置数据,并通过组件 props接收。

    路由传参处理 - 图1

    路由传参处理 - 图2

    <!DOCTYPE html> <html lang=“en”> <head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <title>Document</title> </head> <body> <div id=“app”> <router-link to=“/user/1”>用户1</router-link> <router-link to=“/user/2”>用户2</router-link> <router-link to=“/user/3”>用户3</router-link> <router-link to=“/category/1”>分类1</router-link> <router-link to=“/category/2”>分类2</router-link> <router-link to=“/category/3”>分类3</router-link> <router-view></router-view> </div> <script src=“lib/vue.js”></script> <script src=“lib/vue-router.js”></script> <script> // 组件的配置对象 var User = { template: <div>这是用户 {{ $route.params.id }} 功能</div> }; var Category = { props: [‘id’], template: <div>这是分类 {{ id }} 功能</div> }; // 设置路由规则 var routes = [ { path: ‘/user/:id’, component: User }, { path: ‘/category/:id’, component: Category, props: true } ]; var router = new VueRouter({ routes }); var vm = new Vue({ el: ‘#app’, router }); </script> </body> </html>