【问题标题】:How to use localeRoute from nuxt-i18n package in vuex如何在 vuex 中使用 nuxt-i18n 包中的 localeRoute
【发布时间】:2021-04-22 20:26:57
【问题描述】:
我正在尝试使用来自 nuxt-i18n 的 localRoute 方法
this.$router.push(this.localeRoute({ name: "home" }))
我试过这种方法,但它不起作用,正确的方法是什么?
【问题讨论】:
标签:
vue.js
nuxt.js
vuex
vue-i18n
nuxt-i18n
【解决方案1】:
在 vuex 内部,this.localRoute 或 this.localPath 未定义,因为“this”脱离了上下文。
起作用的是,将整个 localPath 对象传递给操作。
所以在你的方法中你这样做:
test(){
let route = this.localePath({ name: 'forgotPassword' })
this.$store.dispatch('storeTest', route)
},
那么在action里面你可以做的:
storeTest({ commit }, route){
this.app.router.push(route) //this works
$nuxt._router.push(route) //this works as well
},
以同样的方式,如果其他选项不起作用,您可以将整个路由器传递给操作。然后你可以这样做:router.push(route)