Vue.js项目实战-多语种网站(租车)

首先来看一下网站效果,想写这个项目的读者可以自行下载哦,地址:https://github.com/Stray-Kite/Car

在这个项目中,我们主要是为了学习语种切换,也就是右上角的 中文/English 功能的实现。

首先看一下模拟的后台数据src/config/modules/lang.js 文件中:

关键代码:

  1 export default {
  2   name: 'Homepage',
  3   components: {
  4     ScrollNumber
  5   },
  6   data () {
  7     return {
  8       lang: 'chinese',
  9       pageIndex: 0,
 10       stepIndex: 0
 11     }
 12   },
 13   ......其余代码 44   methods: {
 45     addClass (el, _class) {177     //切换语言
178     toggleLang (lang) {
179       this.lang = lang
180       // this.animatePage()
181     }
182   },
183   //以下几个computed是获取config文件夹里的数据
184   computed: {
185     langs () {
186       return config.langs[this.lang]  //主要靠这里切换,这个切换的本质其实就是使用了另一套英文的数据(换了一套后台数据)
187     },
188     ......其余代码198   }
199 }

猜你喜欢

转载自www.cnblogs.com/Trojan00/p/11311544.html