简体   繁体   English

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

[英]How to add custom property in Quasar?

I want to add custom properties in Quasar framework, but when I set it the ESlint shows me this error: Array prototype is read only, properties should not be added我想在 Quasar 框架中添加自定义属性,但是当我设置它时,ESlint 向我显示了这个错误: Array prototype is read only, properties should not be added

I want to add a extend method for Array:我想为 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)
}

When you extend an object, you change its behaviour.当你扩展一个对象时,你改变了它的行为。

Changing the behaviour of an object that will only be used by your own code is fine.更改仅由您自己的代码使用的对象的行为是可以的。 But when you change the behaviour of something that is also used by other code there is a risk you will break that other code.但是,当您更改其他代码也使用的某些内容的行为时,您可能会破坏其他代码。

Your options here can be to create a function and import it:您在此处的选项可以是创建一个函数并将其导入:

helpers.js helpers.js

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

export default extend;

componentA.vue组件A.vue

import extend from './helpers.js';

// use extend as a normal function

or we could be a little bit smarter and use native methods that Javascript already has:或者我们可以更聪明一点,使用 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