【发布时间】:2020-02-07 11:59:33
【问题描述】:
我正在像这样将时刻导入 Vue:
import moment from 'moment-timezone/builds/moment-timezone-with-data-2012-2022';
Vue.prototype.moment = moment;
然后在我的created() 中设置我想要的语言。
请记住,这适用于刷新,但不适用于运行时。
export default new Vue({
render: h => h(BaseApp),
mounted() {
let supportedLanguages = ['tr', 'etc'];
supportedLanguages.forEach((val) => {
moment.locale(val, {
months: this.$i18n.messages[val]._months_,
monthsShort: this.$i18n.messages[val]._months_short_,
monthsParseExact: true,
weekdays: this.$i18n.messages[val]._weekdays_,
weekdaysShort: this.$i18n.messages[val]._weekdays_short_,
weekdaysMin: this.$i18n.messages[val]._weekdays_min_,
weekdaysParseExact: true,
});
})
},
methods: {
changeLocale(lang) {
this.moment.locale(lang);
}
}
})
但是,在我的控制台中,如果我尝试 $vm0.moment.locale() 它会返回正确的语言环境
在我所有的组件中,我都在模板中使用它
<h1>{{ day.format('ddd') }}</h1>
问题是,当我尝试更改时刻的语言环境时,它并没有在每个组件的模板中更改。有没有办法在所有模板中强制刷新。 (我在 root 中尝试了$vm.$forceUpdate())
我怎样才能让时刻变得被动?
【问题讨论】:
标签: javascript vue.js ecmascript-6 momentjs