• vue-i18n使用ES6语法以及空格换行问题


    1.运行报错

    报错使用了不恰当的exports

    Uncaught TypeError : Cannot assign to read only property 'exports ' of object ' # < Object > '

    网上很多教程是写的

    //zh.js
    module.exports={
        part1 : {
                name:'姓名',
                nation:'地区'
        }
         part2 : {
                gender:'性别'
        }
    }
    //lang.js
    import Vue from 'vue';
    import VueI18n from 'vue-i18n';
    Vue.use(VueI18n);
    
    const messages = {
        'zh': require('../lang/zh.js'),   // 中文语言包
        'en': require('../lang/en.js'),    // 英文语言包
    }
        export default new VueI18n({
        locale: 'zh', // set locale 默认显示中文
        fallbackLocale: 'en', //如果语言包没有,则默认从英语中抽取
        messages: messages // set locale messages
    });
    
    

    使用了module.exports以及require,然后运行可能会报错

    点击错误

    原因是:The code above is ok. You can mix require and export. You can‘t mix import and module.exports.
    在webpack打包的时候,可以在js文件中混用require和export。但是不能混用import 以及module.exports。
    因为webpack 2中不允许混用import和module.exports,说是要统一使用es6语法

    所以 ,解决方法:

    require改成import

    module.exports改成export default

    具体可参照 vue-i18n安装配置运行 2,4点

    nice!

    2.字段的空格,换行处理

    使用 v-html将js文件中的字段中包含的符号解析成html能解析的样子

    v-html用于输出html,将内容当成html标签解析后展示

    空格
    zh.js

    export default{
        part1 : {
                name:'姓&nbsp;&nbsp;&nbsp;名',
                nation:'地区'
        }
         part2 : {
                gender:'性别'
        }
    }
    
    

    show.vue

    //wrong
    <div>
      <p>$t{{part1.name}}</p> //展示为姓&nbsp;&nbsp;&nbsp;名
    </div>
    //right
    <div>
      <p   v-html='$t(part1.name)'></p> //展示为姓    名
    </div>
    

    换行
    zh.js

    export default{
        part1 : {
                name:'姓<br>名',
                nation:'地区'
        }
         part2 : {
                gender:'性别'
        }
    }
    
    

    show.vue

    //wrong
    <div>
      <p>$t{{part1.name}}</p> //展示为姓<br>名
    </div>
    //right
    <div>
      <p   v-html='$t(part1.name)'></p> 
    //展示为
    //                      姓    
    //                      名
    </div>
    

    3.匹配多语言某一项

    zh.js

    export default{
        part1 : {
                app0:'你好',
                app1:'您好'
        }
    }
    

    show.vue

    //item.e = 0
    <div>
      <p>{{$t(`part1.app${item.e}`)}}</p> //展示为 你好
    </div>
    
  • 相关阅读:
    I.MX6 RGB clock 和 data 重合
    git 一次删除所有删除的文件
    C# ListView 列宽调整 刷新
    I.MX6 dhcpcd 需要指定网卡
    VS 一些用法设置
    Java写的爬虫的基本程序
    jQuery HighchartsTableHTML表格转Highcharts图表插件
    highcharts 去掉打印和链接
    Highcharts选项配置详细说明文档
    Highcharts使用指南
  • 原文地址:https://www.cnblogs.com/gggggggxin/p/10076166.html
Copyright © 2020-2023  润新知