在组件中,我有两个输入变量
@Input() public data: User[] = []
@Input() public type: UserType = 'A1';
如果以上变量的值发生变化,我需要为此使用以下方法来调用函数
ngOnChanges(changes: SimpleChanges) {
if (changes.type && changes.data) {
this.userList = this.userService.getAllUsersList(changes.type.currentValue, changes.data.currentValue);
}
}
但是我需要更改此设置,我需要使用Setters函数而不是ngOnChanges。以下是我的方法,但出现错误。我评论了直接申报
//@Input() public data: User[] = [] not using this type declaration
// @Input() public type: UserType = 'A1'; not using this type declaration
@Input() set type(value: UserType) {
}
@Input() set data(value: User[] ) {
}
我遇到的问题
1)当我这样做时,无法像public那样定义变量类型,但出现错误
@Input() set public type(value: UserType) {
}
2)无法设置默认值
3)困惑如何调用getAllUsersList
property-watch-decorator
从npm安装。
npm i property-watch-decorator
然后在使用@OnChange
装饰器之前@input
export class TestComponent {
@OnChange(TestComponent.prototype.userList)
@Input() type: UserType = 'A1'
@OnChange(TestComponent.prototype.userList)
@Input data: User[] = [];
private userList() {
if (this.data && this.user.length) {
this.userService.getAllUsersList(changes.type.currentValue, changes.data.currentValue);
}
}
ngAfterViewInit() {
// To handle default values.
this.userList();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句