在使用nuxt
之前,我们正常的route控制语法如下:
const route = new Router({routes:{[...]}
})route.beforeEach(to,from,next){//进行路由权限校验等方法
}
而使用nuxt
,路由默认会根据页面的路径规则自动生成,所以乍一看根本没有配置的地方,所以当我们想要使用类似beforeEach
功能的时候,我们就需要自己定义一个小插件啦。步骤如下:
1、首先在plugins
目录下创建文件route.js
export default ({ app }) => {app.router.afterEach((to, from) => {console.log(to.path)})
}
2、在nuxt.config.js
中plugins
数组增加'~/plugins/route'
这样就搞定啦。