angular2在构造函数中创建一个变量

angular2在构造函数中创建一个变量,angular,Angular,我想在构造函数(或函数)中创建一个变量。我不想在整个课堂上创造。可能吗 这是我的密码 import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { CustomValidators } from 'ng2-validation'; import { TranslatorService } from '.

我想在构造函数(或函数)中创建一个变量。我不想在整个课堂上创造。可能吗

这是我的密码

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { TranslatorService } from '../../../../core/translator/translator.service';
import { CarBrandService } from '../car-brand.service';
import { Response } from '../../../../shared/models/response';
import { SessionService } from '../../../../shared/services/session/session.service';
import { ServerService } from '../../../../shared/services/server/server.service';
import { ToasterService, ToasterConfig } from 'angular2-toaster/angular2-toaster';
import { Router } from '@angular/router';
import { CarBrand } from '../car-brand';
import { SharedModule } from '../../../../shared/shared.module';
import { Table } from '../../../../shared/models/table';
import { TableRow } from '../../../../shared/models/table-row';
import { TableAction } from '../../../../shared/models/table-action';
import { ListTableComponent } from '../../../../shared/components/list-table/list-table.component';

@Component({
  selector: 'car-brand-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss'],
  providers: [CarBrandService, Response, SessionService, ServerService,             ListTableComponent, TableAction, Table, TableRow]
})
export class CarBrandIndexComponent implements OnInit {
  error : String;
  total : number;
  items : Array<CarBrand>;
  table : Table;
  constructor(public translator: TranslatorService, private mainService : CarBrandService, public sessionService : SessionService, public response : Response, private toasterService: ToasterService, private router: Router) {

    var action : TableAction;
    action.value = "/view/";
    action.type = "link";
    this.table.actions.push(action);
    var action : TableAction;
    action.value = "/edit/";
    action.type = "link";
    this.table.actions.push(action);
    this.table.columns = ["MODULES.COMMON.LOGO", "MODULES.COMMON.NAME", "MODULES.COMMON.ACTIONS"];    
    this.getItems(1,10);
  }

  getItems(pageNumber : number, pageSize : number){
//Do something
            });        
        }
        else
          this.error = result.code;
    });
  }
  ngOnInit() {
       }
}
但是在下面这行 var作用:表作用; 它告诉我动作是未定义的。有没有一种方法可以在不必在类下面声明var的情况下解决这个问题

var action : TableAction
这是一份声明

var action : TableAction=new TableAction();

这是一个定义。
您正在尝试设置未定义的
操作
的对象属性

var action:TableAction;你只是说说“动作”类型。现在,如果您尝试将类似{a:1,b:2}的内容分配给操作,您将看到一个错误

但是如果你写var-action:TableAction=new-TableAction()--你会在你的作用域(constructor/function)中有一个你想要的实例,并为变量“action”定义一个类型

建议:使用let而不是var

let action = new TableAction(); 
使用关键字“let”定义的变量的行为更容易预测

constructor() {
  let action1 = new TableAction("/view/", "link");
  this.table.actions.push(action1);

  let action2 = new TableAction("/edit/", "link");
  this.table.actions.push(action2);
  // ...
}

export class TableAction {
  type : String;
  value : String;
  name : String;
  icon: String;

  constructor(value, type) { 
    this.value = value;
    this.type = type;
  }
}

它未定义,因为我认为您从未初始化该值。所以试着让动作:TableAction=newtableAction();
let action = new TableAction(); 
constructor() {
  let action1 = new TableAction("/view/", "link");
  this.table.actions.push(action1);

  let action2 = new TableAction("/edit/", "link");
  this.table.actions.push(action2);
  // ...
}

export class TableAction {
  type : String;
  value : String;
  name : String;
  icon: String;

  constructor(value, type) { 
    this.value = value;
    this.type = type;
  }
}