gpt4 book ai didi

javascript - vue.js:597 [Vue warn]: 属性或方法 "$t"未定义

转载 作者:行者123 更新时间:2023-12-03 02:06:41 24 4
gpt4 key购买 nike

我正在尝试实现 vue-i18n Vue-i18n Github我有一个错误:

vue.js:597 [Vue warn]: Property or method "$t" is not defined

我的 vuejs 2 应用程序工作正常,直到我添加了入门代码,我错在哪里?提前致谢

<div id="app">
<p>{{ $t("message.hello")}}</p>
</div>

<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-i18n/dist/vue-i18n.js"></script>

<script>
const app = new Vue({
el: '#app',
data: {
products: [
'Boots',
]
},
})
</script>
<script>
// Ready translated locale messages
const messages = {
en: {
message: {
hello: 'hello world'
}
},
ja: {
message: {
hello: 'こんにちは、世界'
}
}
}

// Create VueI18n instance with options
const i18n = new VueI18n({
locale: 'ja', // set locale
messages, // set locale messages
})

// Create a Vue instance with `i18n` option
new Vue({ i18n }).$mount('#app')
// Now the app has started!
</script>

最佳答案

您必须在希望 vue-i18n 工作的任何 Vue 实例中指定 i18n

您的第一个实例没有指定 i18n

此外,您有两个 Vue 实例,它们不能一起工作,因此您可能只需要一个(指定 i18n)。

<div id="app">
<p>{{ $t("message.hello")}}</p>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-i18n/dist/vue-i18n.js"></script>
<script>
// Ready translated locale messages
const messages = {
en: {
message: {
hello: 'hello world'
}
},
ja: {
message: {
hello: 'こんにちは、世界'
}
}
}
// Create VueI18n instance with options
const i18n = new VueI18n({
locale: 'ja', // set locale
messages, // set locale messages
})
// Create a Vue instance with `i18n` option
const app = new Vue({
el: '#app',
i18n, // this is equivalent to `i18n: i18n,` (without quotes, naturally)
data: {
products: [
'Boots',
]
},
})
// Now the app has started!
</script>

关于javascript - vue.js:597 [Vue warn]: 属性或方法 "$t"未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com