【问题标题】:How to add custom property in Quasar?如何在 Quasar 中添加自定义属性?
【发布时间】:2020-03-21 21:05:13
【问题描述】:

我想在 Quasar 框架中添加自定义属性,但是当我设置它时,ESlint 向我显示此错误: Array prototype is read only, properties should not be added

我想为 Array 添加一个扩展方法:

Array.prototype.extend = function (other_array) {
    /* You should include a test to check whether other_array really is an array */
    other_array.forEach(function(v) {this.push(v)}, this)
}

【问题讨论】:

    标签: javascript vue.js vuejs2 quasar-framework quasar


    【解决方案1】:

    当你扩展一个对象时,你改变了它的行为。

    更改仅由您自己的代码使用的对象的行为很好。但是,当您更改其他代码也使用的某些内容的行为时,您可能会破坏其他代码。

    您可以在这里选择创建一个函数并将其导入:

    helpers.js

    let extend = function(other_array) {
      return other_array.forEach(function(v) {this.push(v)}, this)
    }
    
    export default extend;
    

    componentA.vue

    import extend from './helpers.js';
    
    // use extend as a normal function
    

    或者我们可以更聪明一点,使用 Javascript 已有的本地方法:

    // will 'glue' two arrays together
    firstArray.concat(secondArray);
    
    // or using new ECMA syntax (spread operator)
    finalArray = [...firstArray, ...secondArray];
    

    【讨论】:

    • 你的解释确实解决了我的顾虑。非常感谢。
    • @AliHallaji 如果答案帮助您找到解决方案,请将其标记为正确
    猜你喜欢
    • 2018-02-26
    • 2015-09-25
    • 1970-01-01
    • 2017-11-07
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多