【发布时间】:2019-09-08 21:01:55
【问题描述】:
我正在尝试在我的@Component 上使用 vuex $store。但它不是被动的:/
示例:
import { Component, Vue } from 'vue-property-decorator';
@Component
export default class Internationalize extends Vue {
protected selectedLanguage: any = this.$store.getters['globalLocale'];
private langages = this.$store.getters['globalLanguages'];
protected switchLanguage(locale: any) {
if (locale !== this.selectedLanguage.locale) {
const newLanguage = this.langages.find((lang: any) => lang.locale === locale);
this.$store.dispatch('updateLocale', newLanguage);
}
}
}
this.$store.dispatch('updateLocale', newLanguage);
状态 globalLanguages 将被更改,但我的变量 selectedLanguage 不是反应性的..
谢谢
编辑:工作得很好
import { Component, Vue } from 'vue-property-decorator';
import { mapGetters } from 'vuex';
@Component({
computed: {
...mapGetters({
selectedLanguage: 'globalLocale',
langages: 'globalLanguages'
})
}
})
export default class Internationalize extends Vue {
protected selectedLanguage!: any;
protected langages!: any;
protected flag = this.$store.getters.globalLocale.flagSuffix;
protected switchLanguage(locale: any) {
if (locale !== this.selectedLanguage.locale) {
const newLanguage = this.langages.find((lang: any) => lang.locale === locale);
this.$store.dispatch('updateLocale', newLanguage).then(() => {
this.$i18n.locale = locale;
this.$i18n.setLocaleMessage(this.$i18n.locale, this.$store.getters.globalTranslations);
this.$i18n.fallbackLocale = process.env.VUE_APP_DEFAULT_LOCALE;
});
}
}
}
【问题讨论】:
-
您是否在控制台中遇到任何错误
标签: typescript vuejs2 vue-component reactive-programming vuex