說(shuō)到vue-i18n,我們并不陌生,國(guó)際化多語(yǔ)言實(shí)現(xiàn)。
1.安裝依賴包
//vue-i18n
npm install vue-i18n
//除了安裝依賴包以外,還可以直接<script>標(biāo)簽直接引入,這種方式自行百度
2.注入vue實(shí)例中,項(xiàng)目中實(shí)現(xiàn)調(diào)用api和模板語(yǔ)法
(1)在main.js中引入vue-i18n
import Vue from 'vue'
import App from './App'
import store from './store'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
Vue.config.productionTip = false
Vue.prototype.$store = store
const i18n = new VueI18n({
locale : 'en-US', //語(yǔ)言標(biāo)識(shí)
messages: {
'en-US' : require('common/lang/en.js') , //英文語(yǔ)言包
'zh-CN' : require('common/lang/zh.js') //中文繁體語(yǔ)言包
}
})
Vue.prototype._i18n = i18n
Vue.prototype.$i18nMsg = function(){
return i18n.messages[i18n.locale]
}
App.mpType = 'app'
const app = new Vue({
i18n, //重要?。。?!
store,
...App
})
app.$mount()
(2)en.js與zh.js
//語(yǔ)言包
export const index = {
//英文語(yǔ)言包
"statistics":'Statistics',
"message":'Message',
......
}
(3)頁(yè)面渲染
//computed監(jiān)聽
computed:{
i18n() {
return this.$i18nMsg()
}
},
//template中使用
{{i18n.index.statistics}}//一般
{{i18n.index[item.name]}}//v-for循環(huán)中
:placeholder="i18n.index.input" //input placeholder提示信息