pem*_*emi 2 laravel vue.js vue-i18n
我正在使用 laravel-vue-i18n-generator 包来处理我的 laravel 项目中 vuejs 组件中的文本翻译。我已经设置了 app.js 如下:
import VueInternationalization from 'vue-i18n';
import Locale from './vue-i18n-locales.generated';
Vue.use(VueInternationalization);
const lang = 'fa';
const i18n = new VueInternationalization({
locale: lang,
messages: Locale
});
const app = new Vue({
el: '#app',
i18n,
});
Run Code Online (Sandbox Code Playgroud)
在组件中:
<template>
<a href="#" class="tip" title="" :title="$t('component.delete.title')" @click.prevent="deleteAction">
<i :class="icon"></i>
</a>
</template>
<script>
import swal from 'sweetalert';
import axios from 'axios';
export default {
inject: ['$i18n'],
props:{
endpoint: {
type: String,
required: true,
},
icon: {
type: String,
default: 'fa fa-trash'
},
message: {
type: String,
default: this.$i18n.t('component.delete.are_you_sure'),
},
confirm: {
type: String,
default: this.$i18n.t('component.delete.confirm'),
},
cancel: {
type: String,
default: this.$i18n.t('component.delete.cancel'),
},
success: {
type: String,
default: this.$i18n.t('component.delete.success'),
},
failed: {
type: String,
default: this.$i18n.t('component.delete.failed'),
},
},
mounted() {
console.log(this);
},
methods:{
deleteAction(){
const vm = this;
swal({
text: this.message,
buttons: {
catch: {
text: this.confirm,
value: "delete",
},
cancel: this.cancel
},
dangerMode: true
}).then(name => {
if (!name) return false;
axios.delete(vm.endpoint)
.then(function (response) {
swal( vm.$i18n.t('component.delete.congrats'),vm.success, 'success').then(() => {
location.reload();
});
})
.catch(function (error) {
swal( vm.$i18n.t('component.delete.error'), vm.failed, 'error');
});
});
}
}
}
</script>
<style scoped>
</style>
Run Code Online (Sandbox Code Playgroud)
幸运的是$t('component.delete.title')在模板部分工作正常,但在脚本部分,我遇到了这个错误:
未捕获的类型错误:无法读取未定义的属性“t”
我哪里出错了?
小智 1
在 vue.js 中,如果出现类似错误
“_vm.translate is not a function” 很可能是您没有导入包含translate方法的i18n包。当您尝试使用v-bind向html属性添加翻译时,有时会出现此错误。例子:
<a-form-item class="mb-0" :label="`${translate('person_name.firstname')}`">
Run Code Online (Sandbox Code Playgroud)
以下步骤可以解决该错误。
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<script lang="ts">
import { translate, i18n } from '@/i18n';
@Component({
components: {
AgIconBase
},
methods:{
translate
}
})
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5891 次 |
| 最近记录: |