Pir*_*App 2 javascript vue-router nuxt.js
nuxt-community/router-modulerouter.js可以添加全局路由配置的文件您可以通过 Nuxt 在外部 Vue 组件中提供的根上下文访问存储window.$nuxt:
// router.js
import Router from 'vue-router'
export function createRouter(ssrContext, createDefaultRouter, routerOptions) {
const options = routerOptions ? routerOptions : createDefaultRouter(ssrContext).options
const router = new Router({
...options
})
if (process.client) {
router.beforeEach((to, from, next) => {
// Since `beforeEach` is invoked before `window.$nuxt` is
// initialized, use `setTimeout` without a timeout to wait
// until the next macro tick.
setTimeout(() => {
window.$nuxt.$store.dispatch('myAction')
})
next()
})
}
return router
}
Run Code Online (Sandbox Code Playgroud)
如果使用模块的唯一原因@nuxtjs/router是添加路由器挂钩,那么您实际上可以使用Nuxt 插件beforeEach来完成此操作:
// plugins/router.js
export default ({ app, store }) => {
app.router.beforeEach((to, from, next) => {
store.dispatch('myAction')
next()
})
}
// nuxt.config.js
export default {
plugins: [
'~plugins/router.js'
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2733 次 |
| 最近记录: |