This commit is contained in:
theluyuan 2021-05-11 17:38:13 +08:00
parent 0d9e720669
commit fcd5b18321
2 changed files with 23 additions and 12 deletions

View File

@ -26,6 +26,7 @@
export default { export default {
// this // this
beforeRouteEnter(to, from, next){ beforeRouteEnter(to, from, next){
console.log(from.meta,"这是在组件内的导航收尾取到的")
console.log("组件") console.log("组件")
next() next()
}, },
@ -45,6 +46,7 @@ export default {
}, },
mounted(){ mounted(){
console.log("生命周期") console.log("生命周期")
console.log(this.$route.meta,"这是在组建内")
}, },
methods:{ methods:{
xiugai(){ xiugai(){

View File

@ -5,16 +5,16 @@ import user from "../views/user.vue"
import admin from "../pages/admin.vue" import admin from "../pages/admin.vue"
Vue.use(VueRouter) Vue.use(VueRouter)
const routes = [ const routes = [
{ path: '/', component: index }, { path: '/', component: index,meta:[{a:1},2,3] },
{ path: '/a/b/c/d', name: "user", component: user }, { path: '/a/b/c/d', name: "user", component: user,meta:"user" },
{ {
path: "/admin", beforeEnter: (to, from, next) => { path: "/admin",meta:"admin", beforeEnter: (to, from, next) => {
// ... // ...
console.log("路由独享") console.log("路由独享")
next() next()
}, component: admin }, component: admin
}, },
{ path: "/params", name: "params", component: () => import("../pages/params.vue") } { path: "/params",meta:"admin", name: "params", component: () => import("../pages/params.vue") } // ()=>import('') 按需加载 路由异步加载
] ]
// path // path
// name 名字 // name 名字
@ -33,16 +33,25 @@ const router = new VueRouter({
// 前置守卫 // 前置守卫
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
// console.log(to,from) // console.log(to,from)
if (to.path == "/") { // this.$route.meta
// alert("你即将到首页") console.log(to.meta,"路由元信息")
console.log("你即将到首页") if(to.meta == "admin"){
} console.log("权限验证")
if (to.path == "/a/b/c/d") { // 权限验证
console.log("到user") next()
next({ path: '/admin', query: { name: 123 } }) }else{
} else {
next() next()
} }
// if (to.path == "/") {
// // alert("你即将到首页")
// console.log("你即将到首页")
// }
// if (to.path == "/a/b/c/d") {
// console.log("到user")
// next({ path: '/admin', query: { name: 123 } })
// } else {
// next()
// }
// ... // ...
// next() // 继续跳转 // next() // 继续跳转
// next(false) 停止跳转 这次跳转不生效 // next(false) 停止跳转 这次跳转不生效