1.利用 {name:'组件名称',params:{参数1:数值,参数2:数值}}
<router-link v-bind:to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
<template> <div class="app"> <el-container> <el-aside class="app-side app-side-left" :class="isCollapse ? 'app-side-collapsed' : 'app-side-expanded'"> <div class="app-side-logo"> <img src="@/assets/logo.png" :width="isCollapse ? '60' : '60'" height="60" /> </div> <div> <!-- 我是样例菜单 --> <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" :collapse="isCollapse"> <el-submenu index="1"> <template slot="title"> <i class="el-icon-location"></i> <span slot="title">系统维护</span> </template> <el-menu-item-group> <span slot="title">分组一</span> <el-menu-item index="1-1">
<!--name:组件名 params:参数,需要对象,用v-bind绑定--> <router-link v-bind:to="{name:'UserProfile',params:{id:1}}">个人信息</router-link> </el-menu-item> <el-menu-item index="1-2"> <router-link to="/user/list">用户列表</router-link> </el-menu-item> </el-menu-item-group> <el-menu-item-group title="分组2"> <el-menu-item index="1-3">选项3</el-menu-item> </el-menu-item-group> <el-submenu index="1-4"> <span slot="title">选项4</span> <el-menu-item index="1-4-1">选项1</el-menu-item> </el-submenu> </el-submenu> <el-menu-item index="2"> <i class="el-icon-menu"></i> <span slot="title">导航二</span> </el-menu-item> <el-menu-item index="3" disabled> <i class="el-icon-document"></i> <span slot="title">导航三</span> </el-menu-item> <el-menu-item index="4"> <i class="el-icon-setting"></i> <span slot="title">导航四</span> </el-menu-item> </el-menu> </div> </el-aside> <el-container> <el-header class="app-header"> <div style="width: 60px; cursor: pointer;" @click.prevent="toggleSideBar"> <i v-show="!isCollapse" class="el-icon-d-arrow-left"></i> <i v-show="isCollapse" class="el-icon-d-arrow-right"></i> </div> <!-- 我是样例菜单 --> <el-menu default-active="1" class="el-menu-demo tab-page" mode="horizontal" @select="handleSelect" active-text-color="#409EFF"> <el-menu-item index="1">处理中心</el-menu-item> <el-submenu index="2"> <template slot="title">我的工作台</template> <el-menu-item index="2-1">选项1</el-menu-item> <el-menu-item index="2-2">选项2</el-menu-item> <el-menu-item index="2-3">选项3</el-menu-item> <el-submenu index="2-4"> <template slot="title">选项4</template> <el-menu-item index="2-4-1">选项1</el-menu-item> <el-menu-item index="2-4-2">选项2</el-menu-item> <el-menu-item index="2-4-3">选项3</el-menu-item> </el-submenu> </el-submenu> <el-menu-item index="3" disabled>消息中心</el-menu-item> <el-menu-item index="4"> <a href="#">订单管理</a> </el-menu-item> </el-menu> <div class="app-header-userinfo"> <el-dropdown trigger="hover" :hide-on-click="false"> <span class="el-dropdown-link"> {{ username }} <i class="el-icon-arrow-down el-icon--right"></i> </span> <el-dropdown-menu slot="dropdown"> <el-dropdown-item>我的消息</el-dropdown-item> <el-dropdown-item>设置</el-dropdown-item> <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </div> </el-header> <el-main class="app-body"> <template> <router-view/> </template> </el-main> </el-container> </el-container> </div> </template> <script> export default { name: 'Container', data() { return { username: '', isCollapse: false } }, methods: { toggleSideBar() { this.isCollapse = !this.isCollapse }, logout: function () { this.$confirm('确认退出?', '提示', {}) .then(() => { sessionStorage.removeItem('user'); this.$router.push('/login'); }) .catch(() => { }); }, handleOpen(key, keyPath) { console.log(key, keyPath); }, handleClose(key, keyPath) { console.log(key, keyPath); }, handleSelect(key, keyPath) { console.log(key, keyPath); }, }, mounted: function () { let user = sessionStorage.getItem('user'); if (user) { this.username = user; } }, } </script> <style> </style>
第一种方式传递和接收参数 {{$route.params.id}}
2.修改router中index.js中路由配置
/** * 配置路由 */ import Vue from 'vue' import Router from 'vue-router' import Main from '../views/Main' import Login from '../views/Login' import UserList from '../views/user/List' import UserProfile from '../views/user/Profile' Vue.use(Router); export default new Router({ routes:[ { path:'/login', component:Login },{ path:'/main', component:Main, children:[ //嵌套路由 { path:'/user/profile/:id',//id的参数和Main.vue传递的要对应 name:'UserProfile',//组件名称,在Main.vue中导航使用 component:UserProfile }, {path:'/user/list',component:UserList} ] } ] });
3.在展示的组件中 Profile.vue中接收传递的参数
<template> <div> <h1>个人信息</h1> {{$route.params.id}} </div> </template> <script> export default { name: "UserProfile" } </script> <style scoped> </style>
第二种方式传递和接收参数 props:true
1.在路由配置中增加 props:true
/** * 配置路由 */ import Vue from 'vue' import Router from 'vue-router' import Main from '../views/Main' import Login from '../views/Login' import UserList from '../views/user/List' import UserProfile from '../views/user/Profile' Vue.use(Router); export default new Router({ routes:[ { path:'/login', component:Login },{ path:'/main', component:Main, children:[ //嵌套路由 { path:'/user/profile/:id', name:'UserProfile', component:UserProfile, props:true }, {path:'/user/list',component:UserList} ] } ] });
2.在接收页面 Profile.vue修改接收方式
<template> <div> <h1>个人信息</h1> {{id}} </div> </template> <script> export default { props:['id'], name: "UserProfile" } </script> <style scoped> </style>
重定向配置
1.在路由配置中修改
/** * 配置路由 */ import Vue from 'vue' import Router from 'vue-router' import Main from '../views/Main' import Login from '../views/Login' import UserList from '../views/user/List' import UserProfile from '../views/user/Profile' Vue.use(Router); export default new Router({ routes:[ { path:'/login', component:Login },{ path:'/main', component:Main, children:[ //嵌套路由 { path:'/user/profile/:id', name:'UserProfile', component:UserProfile, props:true }, {path:'/user/list',component:UserList} ] }, { path:'/goHome', redirect:'/main' } ] });
2.修改Main.vue
<template> <div class="app"> <el-container> <el-aside class="app-side app-side-left" :class="isCollapse ? 'app-side-collapsed' : 'app-side-expanded'"> <div class="app-side-logo"> <img src="@/assets/logo.png" :width="isCollapse ? '60' : '60'" height="60" /> </div> <div> <!-- 我是样例菜单 --> <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" :collapse="isCollapse"> <el-submenu index="1"> <template slot="title"> <i class="el-icon-location"></i> <span slot="title">系统维护</span> </template> <el-menu-item-group> <span slot="title">分组一</span> <el-menu-item index="1-1"> <router-link v-bind:to="{name:'UserProfile',params:{id:1}}">个人信息</router-link> </el-menu-item> <el-menu-item index="1-2"> <router-link to="/user/list">用户列表</router-link> </el-menu-item> </el-menu-item-group> <el-menu-item-group title="分组2"> <el-menu-item index="1-3"> <router-link to="/goHome">回到首页</router-link> </el-menu-item> </el-menu-item-group> <el-submenu index="1-4"> <span slot="title">选项4</span> <el-menu-item index="1-4-1">选项1</el-menu-item> </el-submenu> </el-submenu> <el-menu-item index="2"> <i class="el-icon-menu"></i> <span slot="title">导航二</span> </el-menu-item> <el-menu-item index="3" disabled> <i class="el-icon-document"></i> <span slot="title">导航三</span> </el-menu-item> <el-menu-item index="4"> <i class="el-icon-setting"></i> <span slot="title">导航四</span> </el-menu-item> </el-menu> </div> </el-aside> <el-container> <el-header class="app-header"> <div style="width: 60px; cursor: pointer;" @click.prevent="toggleSideBar"> <i v-show="!isCollapse" class="el-icon-d-arrow-left"></i> <i v-show="isCollapse" class="el-icon-d-arrow-right"></i> </div> <!-- 我是样例菜单 --> <el-menu default-active="1" class="el-menu-demo tab-page" mode="horizontal" @select="handleSelect" active-text-color="#409EFF"> <el-menu-item index="1">处理中心</el-menu-item> <el-submenu index="2"> <template slot="title">我的工作台</template> <el-menu-item index="2-1">选项1</el-menu-item> <el-menu-item index="2-2">选项2</el-menu-item> <el-menu-item index="2-3">选项3</el-menu-item> <el-submenu index="2-4"> <template slot="title">选项4</template> <el-menu-item index="2-4-1">选项1</el-menu-item> <el-menu-item index="2-4-2">选项2</el-menu-item> <el-menu-item index="2-4-3">选项3</el-menu-item> </el-submenu> </el-submenu> <el-menu-item index="3" disabled>消息中心</el-menu-item> <el-menu-item index="4"> <a href="#">订单管理</a> </el-menu-item> </el-menu> <div class="app-header-userinfo"> <el-dropdown trigger="hover" :hide-on-click="false"> <span class="el-dropdown-link"> {{ username }} <i class="el-icon-arrow-down el-icon--right"></i> </span> <el-dropdown-menu slot="dropdown"> <el-dropdown-item>我的消息</el-dropdown-item> <el-dropdown-item>设置</el-dropdown-item> <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </div> </el-header> <el-main class="app-body"> <template> <router-view/> </template> </el-main> </el-container> </el-container> </div> </template> <script> export default { name: 'Container', data() { return { username: '', isCollapse: false } }, methods: { toggleSideBar() { this.isCollapse = !this.isCollapse }, logout: function () { this.$confirm('确认退出?', '提示', {}) .then(() => { sessionStorage.removeItem('user'); this.$router.push('/login'); }) .catch(() => { }); }, handleOpen(key, keyPath) { console.log(key, keyPath); }, handleClose(key, keyPath) { console.log(key, keyPath); }, handleSelect(key, keyPath) { console.log(key, keyPath); }, }, mounted: function () { let user = sessionStorage.getItem('user'); if (user) { this.username = user; } }, } </script> <style> </style>