admin管理员组文章数量:1402806
In my app.js
file I build this so I can use translation in vue:
Vue.prototype.trans = string => _.get(window.i18n, string);
This is working great in my vue files:
{{ trans('translation.name') }}
The problem is that I'm using vuex
and I need to translate some things in a module:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default {
namespaced: true,
state: {
page: 1,
criterias: [
{
name: this.trans('translation.name'),
filter: "life",
active: false
}
}
}
But here this.trans('translation.name')
is not working. How could I get this to work?
In my app.js
file I build this so I can use translation in vue:
Vue.prototype.trans = string => _.get(window.i18n, string);
This is working great in my vue files:
{{ trans('translation.name') }}
The problem is that I'm using vuex
and I need to translate some things in a module:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default {
namespaced: true,
state: {
page: 1,
criterias: [
{
name: this.trans('translation.name'),
filter: "life",
active: false
}
}
}
But here this.trans('translation.name')
is not working. How could I get this to work?
-
A workaround, could be create a sepated .js with your prototype and the
import Vue from 'vue'
and importing this file into your store.js instead ofnode_modules/vue
– PJ.Wanderson Commented Jan 21, 2019 at 16:41 -
Vue.trans('translation.name')
instead ofthis.trans('translation.name')
– ljubadr Commented Jan 21, 2019 at 18:31 - Both not working unfortunately – Jamie Commented Jan 31, 2019 at 10:38
-
Isn't
this._vm.trans
working instead? – P3trur0 Commented Jan 31, 2019 at 15:48 - Nope it does not work :( – Jamie Commented Jan 31, 2019 at 15:59
3 Answers
Reset to default 8 +100I would call mutating Vuex prototype as a bad practice and in this case, it's really not necessary.
Simply create a file called localization.js
and instantiate the i18n plugin in that file. Plus export a named function to return the same i18n instance.
// localization.js
const i18n = new VueI18n({ ... });
export const getLocalization = () => i18n;
export default i18n;
Then in your Vuex module import the getLocalization
function and execute it to get the same i18n
instance and do translations using it.
// vuex-module.js
import Vue from 'vue';
import Vuex from 'vuex';
import { getLocalization } from './localization';
Vue.use(Vuex);
const i18n = getLocalization();
export default {
state: {
criteria: i18n('translation.name'),
},
}
I can suggest you a workaround. You can move the initialization of criterias
from store.js
to your App.vue
, using a mutation.
store.js
state: {
page: 1,
criterias: []
},
mutations: {
setCriterias(state, payload) {
state.criterias = payload;
},
}
App.vue
beforeMount() {
this.$store.mit( 'setCriterias' , [
{
name: this.trans('translation.name'),
filter: "life",
active: false
}
])
}
As your translation content window.i18n
is global anyhow, why not define another trans
method similar to the one you add to Vue.prototype
in your store module?
If you are concerned about having to edit this method in two places later on, define a translation module with this one method and use it on both places, where you set Vue.prototype and inside your store module.
本文标签: javascriptMake prototype accessible in vuexStack Overflow
版权声明:本文标题:javascript - Make prototype accessible in vuex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744374330a2603183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论