zoukankan      html  css  js  c++  java
  • vue-i18n输入不同语言的url实现切换语言

    1、安装

    npm install vue-i18n --save
    

      

    2、注入 vue 实例中,项目中实现调用 api 和 模板语法

    // main.js
    // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router/index' import VueI18n from 'vue-i18n'; Vue.config.productionTip = false; Vue.use(VueI18n); /* eslint-disable no-new */ router.beforeEach((to, from, next) => { if ((to.query.lang !== 'cn' && to.query.lang !== 'en')) { if (from.query.lang === 'cn' || from.query.lang === 'en') { to.query.lang = from.query.lang } else { to.query.lang = 'en' } // to.query.profile = from.query.profile; // to.query._ = Date.parse(new Date()) next({ path: to.path, name: to.name, query: to.query, params: to.params, }) }else{ next() } }); const lang = window.location.search.includes('lang=cn') ? 'cn' : (window.location.search.includes('lang=en') ? 'en' : 'jp') ; // console.log('ffffffffff', lang); const messages = { cn: { hello: '好好学习,天天向上!' }, en: { hello: 'good good study, day day up!' }, jp: { hello: 'よく勉強して、毎日向上します!' } }; const i18n = new VueI18n({ locale: lang, // 语言标识 messages, }); new Vue({ el: '#app', router, i18n, components: { App }, template: '<App/>' });

      

    3、在页面中应用

    <template>
      <div class="hello">
    <!--    <h1>{{ msg }}</h1>-->
        <h1 style="font-size: 16px; text-align: center;">{{ $t("hello") }}</h1>
        <h1 style="font-size: 16px; text-align: center;">{{ $t("title") }}</h1>
      </div>
    </template>
    
    <script>
    export default {
      name: 'HelloWorld',
      i18n:{
          messages:{
            en: {
              title: 'Sport Brands',
              nike: 'Nike',
              adi: 'Adidas',
              nb: 'New Banlance',
              ln: 'LI Ning'
            },
            cn: {
              title: '运动品牌',
               nike: '耐克',
                adi: '阿迪达斯',
                nb: '新百伦',
                ln: '李宁'
            },
          }
      },
      data () {
        return {
          msg: 'Welcome to Your Vue.js App'
        }
      }
    }
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped>
    h1, h2 {
      font-weight: normal;
    }
    ul {
      list-style-type: none;
      padding: 0;
    }
    li {
      display: inline-block;
      margin: 0 10px;
    }
    a {
      color: #42b983;
    }
    </style>
    

      

    4、实际效果

  • 相关阅读:
    HTML(图像img、表格table、列表)
    HTML(标题h、段落p、文本格式化、链接a、头部head)
    List的复制 (浅拷贝与深拷贝)
    最新CentOS6.5安装Docker, 使用阿里云源下载(亲测)
    VirtualBox安装CentOS6.5
    P1010 幂次方 题解
    P1469 找筷子 题解
    P1866 编号 题解
    EasyNVR通道离线但视频流可正常播放是什么原因导致的?
    EasyNVR通过国标GB28181协议级联出现报错及播放不了的问题调整
  • 原文地址:https://www.cnblogs.com/chen55555/p/12125306.html
Copyright © 2011-2022 走看看