1.返回上一步
cancel(){
this.$router.go(-1)
}
2.路由元信息的使用
1.元信息改变ititle
这里我们将每个页面的 title 都写在 meta 中来统一维护
const router = new Router({
routes: [
{
path: '/',
name: 'index',
component: index,
meta: {
title: '首页'
}
},
{
path: '/hello',
name: 'hello',
component: hello,
meta: {
title: '欢迎页'
}
}
]
})
三种使用方法
1.
<template>
<div>
<h1>{{$route.meta.title}}</h1>
</div>
</template>
2.
router.beforeEach((to, from, next) => {
window.document.title = to.meta.title;
next();
})
3.动态改变,重新赋值
this.$route.meta.title = "还是首页";
2.通过 meta 定义要验证的路由
const router = new Router({
routes: [
{
path: '/user',
component: User,
children: [
{
path: '',
name: 'user',
component: Profile,
meta: { requiresAuth: true }
},
{
path: 'cart',
name: 'user-cart',
component: Cart,
meta: { requiresAuth: true }
}
]
}
]
})
const isLogin = true;
router.beforeEach((to, from, next) => {
NProgress.start()
if (to.meta.requiresAuth && !isLogin) {
next({
name: 'login',
})
} else {
next()
}
})