TSLint 无法识别订阅方法中数组的更新
TSLint not recognizing updating of array inside subscribe method
在我的 Angular 项目中,当我编写以下代码时,我从 TSLint 收到 不匹配的查询和更新 collection 错误:
export class SomeComponent implements OnInit {
private wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
this.wishes = wishes;
}
}
我得到一个 collection'wishes' 的内容被查询,但从未更新(我在代码中进一步查询)。但是,我不明白为什么我会收到错误消息,因为我每次收到订阅的愿望时都会更新 collection。它与订阅块内发生的更新有关吗?
提前致谢。
删除私人。如果您在全局变量中使用 private 并且没有 getter 和 setter 则不是好的做法。
export class SomeComponent implements OnInit {
wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
this.wishes = wishes;
}
}
}
使用 getter 和 Setter:
export class SomeComponent implements OnInit {
private wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
setWishes(wishes);
}
}
getWishes(): Wish[] {
return this.wishes;
}
setWishes(wishes: Wish[]) {
this.wishes = wishes;
}
}
根据您使用的服务器 运行 它可能会给您带来产品问题,通过使用 privates,所以我会坚持第一个解决方案。
您可以运行 ng build --prod
测试产品代码
在我的 Angular 项目中,当我编写以下代码时,我从 TSLint 收到 不匹配的查询和更新 collection 错误:
export class SomeComponent implements OnInit {
private wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
this.wishes = wishes;
}
}
我得到一个 collection'wishes' 的内容被查询,但从未更新(我在代码中进一步查询)。但是,我不明白为什么我会收到错误消息,因为我每次收到订阅的愿望时都会更新 collection。它与订阅块内发生的更新有关吗? 提前致谢。
删除私人。如果您在全局变量中使用 private 并且没有 getter 和 setter 则不是好的做法。
export class SomeComponent implements OnInit {
wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
this.wishes = wishes;
}
}
}
使用 getter 和 Setter:
export class SomeComponent implements OnInit {
private wishes: Wish[];
constructor(private service: Service) {}
ngOnInit() {
this.service.subscribe(wishes => {
setWishes(wishes);
}
}
getWishes(): Wish[] {
return this.wishes;
}
setWishes(wishes: Wish[]) {
this.wishes = wishes;
}
}
根据您使用的服务器 运行 它可能会给您带来产品问题,通过使用 privates,所以我会坚持第一个解决方案。
您可以运行 ng build --prod
测试产品代码