【问题标题】:Set vue-router messages via axios call通过 axios 调用设置 vue-router 消息
【发布时间】:2017-09-16 12:41:11
【问题描述】:

我想通过对我的后端应用程序的 api 调用来获取我的翻译消息。在axios 调用的then 回调中,我想设置消息。

但是问题似乎是 VueRouter 已经使用空翻译进行了初始化,并且没有从 then 回调中重新加载新的翻译。

我有办法做到这一点吗?

我试过了:

let messages = {};
window.axios.get(route('api.translation.translations.all'))
    .then(response => {
        messages = {
            [currentLocale]: response.data,
        }
    });
console.log(messages);

const i18n = new VueI18n({
    locale: currentLocale, 
    messages, 
});

这不起作用,因为在消息完全设置之前调用了 new VueI18n

window.axios.get(route('api.translation.translations.all'))
    .then(response => {
        messages = {
            [currentLocale]: response.data,
        };
        console.log(messages);
        app.$i18n.setLocaleMessage('en', messages)
    });

这也行不通。

谢谢!

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    找到了解决方案,我不需要指定语言环境,因为它是setLocaleMessage 的第一个参数。

    window.axios.get(route('api.translation.translations.all'))
        .then(response => {
            messages = response.data;
    
            app.$i18n.setLocaleMessage(currentLocale, messages);
        });
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2021-04-06
      • 2017-10-19
      • 2017-05-29
      • 2018-04-01
      • 1970-01-01
      • 2019-02-17
      • 2019-03-27
      • 2019-09-25
      相关资源
      最近更新 更多