简体   繁体   中英

Access Nuxt.js context in Vue plugin

I'm using Nuxt.js and I have two plugins installed. I need access to the VueI18n instance from lang.js in validate.js

Does someone know how to do this?


lang.js

Vue.use(VueI18n)

export default ({ app }) => {
  app.i18n = new VueI18n({
    locale: 'en',
    messages
  })
}

validate.js

Vue.use(VeeValidate, {
  i18nRootKey: 'validations',
  i18n,  // access the VueI18n instance from plugin above
  dictionary: {
    en: validationMessages
  }
})

export default ({ app }) => {
  // This way I could get the instance but how to add it to the plugin?
  console.log(app.i18n)
}

Just move your vue.use inside export default

export default ({ app }) => {

Vue.use(VeeValidate, {
  i18nRootKey: 'validations',
  i18n: app.i18n,  // access the VueI18n instance from plugin above
  dictionary: {
    en: validationMessages
  }
})
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM