【发布时间】:2017-09-29 19:46:01
【问题描述】:
我正在尝试使用 typescript 2.0.3 创建一个类,但我遇到了一些问题,我不知道为什么。
这是我的代码
import { Component, OnInit } from '@angular/core';
import {Car} from '../interfaces/car';
class PrimeCar implements Car
{
constructor(public vin, public year, public brand, public color) {}
}
@Component({
selector: 'rb-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
displayDialog: boolean;
car: Car = new PrimeCar(null, null, null , null);
selectedCar: Car;
newCar: boolean;
cars: Car[];
constructor() { }
ngOnInit() {
this.cars = [ {vin: '111', year: '5554' , brand: '5646' , color: '6466' },
{vin: '111', year: '5554' , brand: '5646' , color: '6466' },
{vin: '111', year: '5554' , brand: '5646' , color: '6466' },
{vin: '111', year: '5554' , brand: '5646' , color: '6466' }
];
}
showDialogToAdd() {
this.newCar = true;
this.car = new PrimeCar(null, null, null, null);
this.displayDialog = true;
}
save() {
const cars = [...this.cars];
if (this.newCar) {
cars.push(this.car);
} else {
cars[this.findSelectedCarIndex()] = this.car;
}
this.cars = cars;
this.car = null;
this.displayDialog = false;
}
delete() {
const index = this.findSelectedCarIndex();
this.cars = this.cars.filter((val, i) => i !== index);
this.car = null;
this.displayDialog = false;
}
onRowSelect(event) {
this.newCar = false;
this.car = this.cloneCar(event.data);
this.displayDialog = true;
}
cloneCar(c: Car): Car {
const car = new PrimeCar(null, null, null, null);
for (let prop: string in c) {
car[prop] = c[prop];
}
return car;
}
findSelectedCarIndex(): number {
return this.cars.indexOf(this.selectedCar);
}
}
在cloneCar方法中,我在尝试写入时出现这个错误:
for (let prop: string in c) {
...}
Tslint:标识符 'prop' 永远不会重新分配,使用 const 代替 让
这是从我的 IDE 捕获的图像: see error here
注意:我在 Angular 项目版本 2.3.0 中使用此代码
请帮忙!
【问题讨论】:
-
这只是一个 linting 错误,它告诉你使用 const 而不是 let in ,因为你永远不会重新评估 prop 到另一个值。
-
还是有问题,是不是打字稿版本不对?
-
不,这是 tslint 给你的风格指示.. palantir.github.io/tslint
-
遵循这个规则是好的。它使代码更易于阅读和维护。
-
是的,我还在寻找解决方案
标签: javascript angular typescript