Vue学习之路由vue-router传参及嵌套小结(十)
一、路由传递参数:
1、使用query传值:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>路由</title> <script src="./lib/vue.js"></script> <!-- 1.安装vue-router路由模块 --> <script src="./lib/vue-router.js"></script> </head> <body> <div id="app"> <!-- 如果在路由中,使用 查询字符串,给路由传递参数,则不需要修改路由规则的path属性 --> <router-link to="/login?id=10&name=zs">登录</router-link> <router-link to="/register">注册</router-link> <router-view></router-view> </div> <script> var login = { template: "<h1>登录--{{ $route.query.id }}--{{ $route.query.name }}</h1>", data() { return { msg: "123" }; }, creadtd() { //组件的生命周期钩子函数 // console.log(this.$route); console.log(this.$route.query.id); } }; var register = { template: "<h1>注册组件</h1>" }; var routerObj = new VueRouter({ routes: [ { path: "/login", component: login }, { path: "/register", component: register } ] }); var vm = new Vue({ el: "#app", data: {}, methods: {}, router: routerObj }); </script> </body> </html>
2、使用params 传参:
地址上需要设置下第二个参数ID
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>路由</title> <script src="./lib/vue.js"></script> <!-- 1.安装vue-router路由模块 --> <script src="./lib/vue-router.js"></script> </head> <body> <div id="app"> <!-- 如果在路由中,使用 查询字符串,给路由传递参数,则不需要修改路由规则的path属性 --> <router-link to="/login/12">登录</router-link> <router-link to="/register">注册</router-link> <router-view></router-view> </div> <script> var login = { template: "<h1>登录--{{ $route.params.id }}</h1>", data() { return { msg: "123" }; }, creadtd() { //组件的生命周期钩子函数 console.log(this.$route.params.id); } }; var register = { template: "<h1>注册组件</h1>" }; var routerObj = new VueRouter({ routes: [ // 除了上面的字母的不同外,这里还需要设置下添加的第二个参数id { path: "/login/:id", component: login }, { path: "/register", component: register } ] }); var vm = new Vue({ el: "#app", data: {}, methods: {}, router: routerObj }); </script> </body> </html>
二、嵌套路由children:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>路由</title> <script src="./lib/vue.js"></script> <!-- 1.安装vue-router路由模块 --> <script src="./lib/vue-router.js"></script> </head> <body> <div id="app"> <router-link to="/account">Account</router-link> <router-view></router-view> </div> <template id="tmp1"> <div> <h1>这是Account组件</h1> <router-link to="/account/login">登录</router-link> <router-link to="/account/register">注册</router-link> <router-view></router-view> </div> </template> <script> var account = { template: "#tmp1" }; var login = { template: "<h3>登录组件</h3>" }; var register = { template: "<h3>注册组件</h3>" }; var router = new VueRouter({ routes: [ { path: "/account", component: account, // 使用children 属性,实现子路由,同时子路由的path前面,不要带/, // 否则永远以根路径开始请求,这样不方便我们用户去理解URL地址 children: [ { path: "login", component: login }, { path: "register", component: register } ] } ] }); var vm = new Vue({ el: "#app", data: {}, methods: {}, router }); </script> </body> </html>