vue-i18n中英切换插件使用
2019-02-14 本文已影响0人
周郭郭先生
虽然现在的浏览器都能实现翻译功能,但没有什么是比甲方爸爸说话更有权威的了。
问:如果实现后端数据传送前端时中英切换?
答:可以通过请求头设置一个为lang的值传送给后台,然后后台对lang=1进行判断,如果值为1表示英文。如果没有值,表示中文,然后进行数据的更改传送。
问:那不是后台传送的数据呢?
答:可通过vue-i8n插件实现。
问:如何通过按钮点击或者时间触发进行中英切换?
答:可以设置h5本地储存,触发事件时将lang进行更改。
问:为什么要用h5本地储存?
答;cookie也可以,看个人习惯。
1、安装
npm install vue-i18n 或 yarn add vue-i18n
2、建新utils文件夹并建新storage.js用于储存当前语言标识符


3、在src文件夹中建新i18n文件夹

4、index.js中添加代码

5、cn.js中以下代码

6、en.js中以下代码

注:en.js与cn.js对象中的key一定要对应,否则会出错
7、在main.js中挂载i18n

8、在项目中如何运用
template标签中使用

script标签中使用

9、axios设置请求头

10、触发事件切换中英文
事件触发后进行页面刷新,不然无法触发ajax请求,那后台就不能切换后的数据重新发过来,加上time参数是为了个别优化。
