繁体   English   中英

如何在 Quasar 中添加自定义属性?

[英]How to add custom property in Quasar?

我想在 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)
}

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

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

您在此处的选项可以是创建一个函数并将其导入:

helpers.js

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

export default extend;

组件A.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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM