所以我对此感到有点疯狂。我真的不明白。
这是我的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"
}
现在我收到了这个错误:
TypeError:无法重新定义属性:$ 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”)和惊喜,第一个返回“undefined”,第二个因为错误而永远不会到达。
发生了什么事?有人知道吗?