这次给大家带来在Vue中使用vue-i18插件实现多语言切换,在Vue中使用vue-i18插件实现多语言切换的注意事项有哪些,下面就是实战案例,一起来看一下。
step1: 在项目中安装vue-i18插件
cnpm install vue-i18n --save-dev
step2:在项目的入口文件main.js中引入vue-i18n插件
import Vue from 'vue'
import router from './router'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({
locale: 'zh', // 语言标识
messages: {
'zh': require('./assets/lang/zh'),
'en': require('./assets/lang/en')
}
})
// vue实例中引入
/* eslint-disable no-new */
new Vue({
el: '#app',
i18n,
router,
template: '<Layout/>',
components: {
Layout
},
})step3:页面中使用
在模板中使用时,大概有以下3种情况,若有疏漏,望大家指正
zh.js
module.exports = {
menu : {
home:"首页"
},
content:{
main:"这里是内容"
}
}en.js
module.exports = {
menu : {
home:"home"
},
content:{
main:"this is content"
}
}1)在标签内作为正文内容
<p class="title">{{$t('menu.home')}}</p>2)作为标签属性使用
<input :placeholder="$t('content.main')" type="text">3)作为js中文字使用时
console.log(this.$t('content.main'));4)待补充...
step4:页面上进行中英文切换,在中英文切换的按钮上绑定事件,如下:
tabEn: function () {
this.$i18n.locale = 'en'
},
tabCn: function () {
this.$i18n.locale = 'zh'
}至此,vue-i18n插件使用完结。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是在Vue中使用vue-i18插件实现多语言切换的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号