[英]Is there any way to get current nuxt.js middleware of page?
我很好奇我們是否可以從任何組件到達當前頁面的中間件?
中間件在組件之前執行,並且不會以任何方式在組件內初始化。
更新:在auth
商店中保留中間件的 state。
export const state = () => ({
isPrivate: false,
})
export const mutations = {
setPrivatePage(state, value) {
state.isPrivate = value
},
}
export const actions = {
privatePage({ commit }) {
commit('setPrivatePage', true)
},
publicPage({ commit }) {
commit('setPrivatePage', false)
},
}
在middleware/auth
中調度操作以設置isPrivate
:
await store.dispatch('auth/privatePage')
我添加了另一個名為public
的中間件將其設置回false
export default async ({ store }) => {
await store.dispatch('auth/publicPage')
}
最后要在每條路由上觸發public
,將其添加到nuxt.config.js
:
// Router
router: {
middleware: 'public',
},
您可以訪問它
$router.matched.meta.middleware
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.