【问题标题】:i18n for Vue2 in Laravel8Laravel 8 中 Vue 2 的国际化
【发布时间】:2023-01-10 11:54:16
【问题描述】:
我将 i18n vue add i18n 添加到我的 Laravel 8 应用程序(使用 vue 2)。我在 app.js 中注册了 i18n:
import Vue from 'vue';
import VueI18n from 'vue-i18n'
Vue.use(VueI18n);
在我试过的 vue 组件的模板中:{{ $t("message") }}
但我得到了错误
[Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading '_t')"
【问题讨论】:
标签:
laravel
vuejs2
internationalization
【解决方案1】:
要在 Vue 模板中使用 $t 函数,您需要将其作为属性传递给 Vue 实例。您可以这样做:
import Vue from 'vue';
import VueI18n from 'vue-i18n';
Vue.use(VueI18n);
const i18n = new VueI18n({
// configuration
});
new Vue({
i18n,
render: h => h(App)
}).$mount('#app');
然后,在 Vue 组件的模板中,你可以像这样使用 $t 函数:
<template>
<div>
{{ $t("message") }}
</div>
</template>
确保您已使用适当的消息正确配置了 i18n 对象。您可以在 Vue I18n 文档中找到有关如何执行此操作的更多信息:https://kazupon.github.io/vue-i18n/