所以可以說我想{{ $t('index-p1') }} 在title.
items: [
{
icon: 'mdi-apps',
title: 'Welcome',
to: '/'
},
我將它用于 i18n,其中{{ $t('index-p1') }}包含的資料存盤在 JSON 檔案中。如果我在 Vue 模板檔案中使用它,它作業得很好,但不是在資料中,我不記得如何做這部分。
uj5u.com熱心網友回復:
items應定義為計算屬性,并$t使用以下方式訪問this:
computed:{
items(){
return [
{
icon: 'mdi-apps',
title: this.$t('welcome'),
to: '/'
}
]
}
}
uj5u.com熱心網友回復:
您可以使用計算屬性并通過以下方式呼叫它this.$t:
const messages = {
en: {
message: {
hello: 'hello world'
}
},
fr: {
message: {
hello: 'bonjour'
}
}
}
const i18n = new VueI18n({
locale: 'en',
messages,
})
new Vue({
i18n,
computed: {
text() { return this.$t("message.hello")}
},
methods: {
setLang(lang) {
i18n.locale = lang
}
}
}).$mount('#demo')
items: [
{
icon: 'mdi-apps',
title: 'Welcome',
to: '/'
},
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-i18n/8.15.5/vue-i18n.min.js"></script>
<div id="demo">
<button @click="setLang('fr')">fr</button>
<button @click="setLang('en')">en</button>
<p>{{ $t("message.hello") }}</p>
{{ text }}
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510585.html
