如何在 vuex 中使用来自 nuxt-i18n 包的 localeRoute

问题描述

我正在尝试使用来自 nuxt-i18n

的 localRoute 方法

this.$router.push(this.localeRoute({ name: "home" }))

我试过这种方法但它不起作用,正确的方法是什么?

解决方法

在 vuex 中,this.localRoute 或 this.localPath 未定义,因为“this”脱离了上下文。

有效的是将整个 localPath 对象传递给操作。

所以在你的方法中你这样做:

test(){
      let route = this.localePath({ name: 'forgotPassword' })
      this.$store.dispatch('storeTest',route)
    },

然后在您可以执行的操作中:

  storeTest({ commit },route){

     this.app.router.push(route) //this works
     $nuxt._router.push(route) //this works as well
  },

同样,如果其他选项不起作用,您可以将整个路由器传递给操作。然后你可以这样做:router.push(route)