vue中使用 vue-i18n 實現國際化 $t


1、安裝

npm install vue-i18n

2、import

import VueI18n from 'vue-i18n'

3、掛載

Vue.use(VueI18n)

4、創建實例

const vuei18n = new VueI18n({
    locale: 'zh', 
    messages: {
      'zh': require('./static/lang/zh'),
      'en': require('./static/lang/en')
    }
})

5、對應語言包示例

export const zh = {
  name: '小明',
  type: '美麗的女孩'
}
export const en = {
  name: 'Bob',
  type: 'Pretty girl'
}

6、綁定到模板

<span>{{$t('someThing')}}</span> //此處的someThing是變量名

7、在vue方法中切換中英文

this.$vuei18n.locale = 'en'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM