在Nuxt应用布局访问CTX财产

问题描述 投票:0回答:1

我在开发模式SSR的Nuxt应用。

当我使用中间件设置isMobile财产上的CTX我希望能够访问它在我的布局。

问题是,我认为nuxtServerInit是中间件之前,所以我不能承诺将值VUE店执行..

这是我使用的中间件,设置isMobile上CTX:

export default function (ctx) {
   const userAgent = ctx.req ? ctx.req.headers['user-agent'] : navigator.userAgent
   ctx.isMobile = /mobile/i.test(userAgent)
}

这就是我如何试图赶上信息:

export const actions = {
   nuxtServerInit ({ dispatch }, ctx) {
      dispatch('app/setIsMobile', ctx.isMobile) // ctx.isMobile is always undefined
   }
}

任何想法如何与我的我的布局里面的属性来访问CTX?

nuxt.js nuxt
1个回答
0
投票

我已经找到一种方法来做到这一点,只需指派到从中间件存储:

export default function ({ store, req }) {
   const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
   const isMobile = /mobile/i.test(userAgent)
   dispatch('app/setIsMobile', isMobile)
}
© www.soinside.com 2019 - 2024. All rights reserved.