gpt4 book ai didi

angular2 在构造函数中创建变量

转载 作者:行者123 更新时间:2023-12-03 00:28:24 25 4
gpt4 key购买 nike

我想在构造函数(或函数)内创建一个变量。我不想在整个类(class)中创造。这可能吗?

这是我的代码

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

这是表操作

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

但是在下面的行中 var 操作:表操作;它告诉我操作未定义。有没有办法解决这个问题,而无需在类下面声明 var?

最佳答案

var 操作:表操作;您只需讲述“行动”类型。现在,如果您尝试将类似 {a: 1, b: 2} 的操作分配给操作,您将看到错误。

但是如果你写 var action : TableAction = new TableAction() - 你将在你的范围(构造函数/函数)中拥有一个你想要的实例,并为变量“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;
}
}

关于angular2 在构造函数中创建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42202950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com