Home > Web Front-end > JS Tutorial > body text

Combining vue and vue-i18n to implement multi-language switching method for background data

亚连
Release: 2018-05-31 17:09:55
Original
2811 people have browsed it

Below I will share with you a method of combining vue and vue-i18n to achieve multi-language switching of background data. It has a good reference value and I hope it will be helpful to everyone.

Define the function in the XXX.js file:

getUser(context,info){
  context.$http.get(SERVER_URL+'/users',info).then(function(data){
   let err =data.body.error;
   if(err===0){
    let dataObj = data.body.userLists;  //获取后台返回的数据
    this.users = dataObj.items.map(function (e,i) {  //遍历获取的数据,用this.$t()将每项数据与翻译资源对应
     e.gender=context.$t(e.gender);            //context 是this, gender 与 diabetes_type 为每个items里的key;gender里的value有三种:'GDRNF'、‘GDRF'、‘GDRM'
     e.diabetes_type = context.$t(e.diabetes_type);
     return e;
    });
    this.listLoading = false;
    // console.log(dataObj);
   }
  })
 },
Copy after login

Then you can call the function in the vue component: XXX.getUser(this,info ); Put the data obtained in the background into the users array after performing corresponding operations;

The above method is to traverse the data obtained through the map function, and use this.$t() to compare the value of items with the value in the translation resource value corresponding to achieve multi-language switching of background data;

Part of the data in the en.json translation resource:

{ 
  "GDRNF":"Not Fill", 
  "GDRF":"Female", 
  "GDRM":"Male", 
 }
Copy after login

The above is me I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

$set and array update methods in vue.js_vue.js

JQuery selects the select component to be selected Value method

Method for code splitting in react-router4 (based on webpack)

The above is the detailed content of Combining vue and vue-i18n to implement multi-language switching method for background data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template