首页 > 解决方案 > Vue I18n - 类型错误:无法重新定义属性:$i18n

问题描述

所以我对此有点疯狂。我真的不明白。

这是我的 app.js 文件的最小版本:

import Vue from 'vue'
import VueI18n from 'vue-i18n'

console.log("vue.prototype", Vue.prototype.$i18n)
Vue.use(VueI18n)
console.log("vue.prototype", Vue.prototype.$i18n)

const createApp = function() {


  // create store and router instances
  const store = createStore()
  const router = createRouter()

  if(process.browser) {
    if(window.__INITIAL_STATE__) {
      store.replaceState(window.__INITIAL_STATE__)
    }
  }

  // sync the router with the vuex store.
  // this registers `store.state.route`
  sync(store, router)

  // create the app instance.
  // here we inject the router, store and ssr context to all child components,
  // making them available everywhere as `this.$router` and `this.$store`.
  // 
  const app = new Vue({
    router,
    store,
    render: h => h(Application)
  })

  // expose the app, the router and the store.
  // note we are not mounting the app here, since bootstrapping will be
  // different depending on whether we are in a browser or on the server.
  return { app, router, store }
}

export { createApp }

如您所见,我只在代码中添加了 Vue.use(VueI18n)。我在用着:

{
"vue-i18n": "^7.6.0"
}

现在我收到此错误:

类型错误:无法重新定义属性:$i18n

出现此错误的行是源代码中的此函数:

function install (_Vue) {
  Vue = _Vue;

  var version = (Vue.version && Number(Vue.version.split('.')[0])) || -1;
  /* istanbul ignore if */
  if (process.env.NODE_ENV !== 'production' && install.installed) {
    warn('already installed.');
    return
  }
  install.installed = true;

  /* istanbul ignore if */
  if (process.env.NODE_ENV !== 'production' && version < 2) {
    warn(("vue-i18n (" + (install.version) + ") need to use Vue 2.0 or later (Vue: " + (Vue.version) + ")."));
    return
  }

  console.log("VUE:PROTOTYPE", Vue.prototype.$i18n)

  Object.defineProperty(Vue.prototype, '$i18n', {
    get: function get () { return this._i18n }
  });

  console.log("VUE:PROTOTYPE", Vue.prototype.$i18n)

  extend(Vue);
  Vue.mixin(mixin);
  Vue.directive('t', { bind: bind, update: update });
  Vue.component(component.name, component);

  // use object-based merge strategy
  var strats = Vue.config.optionMergeStrategies;
  strats.i18n = strats.methods;
}

我添加了两个 console.log("VUE:PROTOTYPE") ,令人惊讶的是,第一个返回“未定义”,而第二个由于错误而从未到达。

怎么了?有人有线索吗?

标签: vuejs2vue-i18n

解决方案


推荐阅读