//meta字段,一个路由匹配到的所有路由记录会暴露为$route
对象(还有在导航守卫中的路有对象)的$route.matched
数组。 const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, children: [ { path: 'bar', component: Bar, // a meta field meta: { requiresAuth: true } } ] } ] })
//遍历$route.matched
来访问路由记录中的meta
字段
//全局导航守卫中检查元字段 router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { // this route requires auth, check if logged in // if not, redirect to login page. if (!auth.loggedIn()) { next({ path: '/login', query: { redirect: to.fullPath } }) } else { next() } } else { next() // 确保一定要调用 next() } })
Store中至少要注入两项,state 和 mutation。 state就是根据你项目的需求,自己定义一个数据结构。 const state = { currentPage: 1, user: '0121213', change: 0, page, ctrl, meta, configs, datas } export default new Vuex.Store({ state, mutations, })