![](/img/trans.png)
[英]Wait for axios API call in vuex to finish before continuing vue-router guard
[英]Set vue-router messages via axios call
我想通過對后端應用程序的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)
});
這也不起作用。
謝謝!
找到解決方案后,我不需要指定語言環境,因為它是setLocaleMessage
的第一個參數。
window.axios.get(route('api.translation.translations.all'))
.then(response => {
messages = response.data;
app.$i18n.setLocaleMessage(currentLocale, messages);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.