简体   繁体   中英

ES6 calling values from constructor

 class ServiceRecordGridFields{
  constructor(){
    this.allColumns = [
    {
      field:'assemblynumber',
      name:'Assembly',
      visible:false,
      width:175
    },{
      field:'assetid',
      name:'Asset ID',
      visible:false,
      width:110
    }];
  }
  getAllColumns(){

    return this.allColumns;
  }
}
export default ServiceRecordGridFields;

Elsewhere I have

import ServiceRecordGridFields from './_serviceRecordGridFields.js';


class serviceRecordGridsCtrl{

  constructor(){

this._ServiceRecordGridFields = ServiceRecordGridFields;

this._serviceRecordsResolve = serviceRecordsResolve;


  } 

  bclick(){
console.log(this._ServiceRecordGridFields.getAllColumns());
  }
  }

calling the function I get "this._ServiceRecordGridFields.getAllColumns is not a function".

if I add "static" in front of getAllColumns() it returns undefined. What am I doing wrong?

ServiceRecordGridFields是一个类,因此您需要使用该类实例化一个对象:

this._ServiceRecordGridFields = new ServiceRecordGridFields()
class ServiceRecordGridFields {
   constructor() {
     this.allColumns = [{
       field: 'assemblynumber',
       name: 'Assembly',
       visible: false,
       width: 175
     }, {
       field: 'assetid',
       name: 'Asset ID',
       visible: false,
       width: 110
     }];
   }
   getAllColumns() {

     return this.allColumns;
   }
 }

 class serviceRecordGridsCtrl {

   constructor() {

     this._ServiceRecordGridFields = new ServiceRecordGridFields();



   }

   bclick() {
     console.log(this._ServiceRecordGridFields.getAllColumns());
   }
 }

 var test = new serviceRecordGridsCtrl();
 test.bclick();

You need to instantiate both classes to use them.

https://jsfiddle.net/68pep88x/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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