2017-02-13 1 views
1

コンストラクタ(または関数)内に変数を作成したいと思います。私はクラス全体で作成したくありません。それは可能ですか?angular2コンストラクタ内に変数を作成する

これはこれはTableAction

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

しかし、次の行 のvarアクションである私のコード

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() { 
     } 
} 

です:TableAction。 アクションは定義されていません。クラスの下にvarを宣言することなく、これを修正する方法はありますか?

+0

私は思っている値を決して初期化しないので、その定義はありません。ですから、let actionを試してください:TableAction = new TableAction(); – Zyga

答えて

1

var action:TableAction;あなたはちょうど 'アクション'タイプについて教えてください。今あなたが{a:1、b:2}のようなアクションに割り当てようとすると、エラーが表示されます。

でも、あなたが望むようにあなたのスコープ内にインスタンスを持っていて、変数 'action'の型を定義している場合は、var action:TableAction = new TableAction()を記述してください。

アドバイス:使用より予測されている「聞かせて」の代わりにVAR

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; 
    } 
} 
1
var action : TableAction 

は宣言です。

var action : TableAction=new TableAction(); 

または

var action : TableAction = {}; 

は定義です。 定義されていないactionのオブジェクトプロパティを設定しようとしています。

関連する問題