1.首先安装国际化组件vue-i18n
$ npm install vue-i18n --save
2.在main.js中完成注册使用
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({
locale: 'zh', // 语言标识
messages: {
'zh': require('./lang/zh'), // 中文语言包
'en': require('./lang/en') // 英文语言包
}
})
Vue.prototype.i18n = i18n;
3.创建对应的语言包
zh.json
{
"欢迎到来":"欢迎到来",
"btnLang":"切换语言"
}
en.json
{
"欢迎到来":"welcome",
"btnLang":"change lang"
}
4.使用方法 : {{$t('key')}}
<h1>
{{$t('欢迎到来')}}
{{$t('btnLang')}}
</h1>
5.切换语言
i18n.locale = "zh"; //i18n对应的locale属性切换就能变换语言