[英]Angular ViewChild undefined if child extends base class
I started with four components: 我从四个部分入手:
UserListComponent
UserSearchComponent
GroupListComponent
GroupSearchComponent
Both list components reference their corresponding search component via a ViewChild
decorated property. 这两个列表组件都通过ViewChild
装饰的属性引用其相应的搜索组件。 For example, the UserListComponent
has a property defined like: 例如, UserListComponent
的属性定义如下:
@ViewChild(UserSearchComponent)
userSearch: UserSearchComponent;
The two list components and search components were so similar with each other, I started looking into how to have components extend base classes so that I D id not R epeat M yself. 这两个组件列表和搜索组件是互相如此相似,我开始寻找到如何让组件来扩展基类,使I D ID不属于R EPEAT 中号 yself。 I started by creating a base class for the search components with the following declaration: 我首先使用以下声明为搜索组件创建基类:
@Component({
selector: 'td-search',
template: ''
})
export class SearchComponent<T> {
...
}
Once I extended the two search components, my ViewChild
references, in both cases, ended up returning undefined
. 扩展了两个搜索组件后,在两种情况下,我的ViewChild
引用最终都返回undefined
。 For example, here is the UserSearchComponent
's declaration after introducing the base class: 例如,在介绍基类之后,这是UserSearchComponent
的声明:
export class UserSearchComponent extends SearchComponent<UserSearch> {
UserSearch
is just a class that wraps available search parameters as an object/payload to send off to the server. UserSearch
只是一个类,它将可用的搜索参数包装为对象/有效载荷,以发送给服务器。
So the ViewChild
properties work in both cases as long as I don't have a base class for the two search components. 因此,只要我没有两个搜索组件的基类, ViewChild
属性就可以在两种情况下都起作用。 But as soon as I introduce a base class, I can't seem to get a ViewChild
reference of the types of the two search components. 但是,一旦我引入了基类,我似乎就无法获得两个搜索组件类型的ViewChild
引用。
The error I get looks like this: 我得到的错误如下所示:
...and I'm just trying to set a property on the search component once the results come back in the list component by calling this method: ...而我只是想通过调用此方法在结果返回列表组件中后在搜索组件上设置属性:
private resetSearch() {
this.loading = false;
this.userSearch.disabled = false;
}
Can I use base classes with components I intend to use as ViewChild
references? 我可以将基类与打算用作ViewChild
引用的组件一起使用吗?
I made the realization, too, that the ViewChild
is NOT undefined at first... in ngOnInit
of the UserListComponent
, I am subscribing to a searches
property of the UserSearchComponent
(of type EventEmitter<UserSearch>
). 我提出的实现,也使ViewChild
不在第一未定义......在ngOnInit
中的UserListComponent
,我订阅了一个searches
的财产UserSearchComponent
(类型EventEmitter<UserSearch>
This successfully finds the UserSearchComponent
and I confirmed this by logging out the whole object right before making the subscription call: 这成功找到了UserSearchComponent
,我通过在进行订阅调用之前注销整个对象来确认了这一点:
console.log(this.userSearch);
this.userSearch.searches.subscribe((search: UserSearch) => {
this.loading = true;
const params = new UserSearchQueryParams();
params.Login = search.login;
params.Username = search.user;
params.EnabledOnly = search.enabled;
this.users$ = this._users.getUsers(params).pipe(
catchError(this.handleError),
finalize(this.resetSearch)
);
});
In the finalize
operator of the call to getUsers
observable, I call resetSearch
which is where this.userSearch
is returning undefined. 在对getUsers
observable的调用的finalize
运算符中,我调用resetSearch
,这是this.userSearch
返回未定义的地方。 So before I do the search, the search component child reference works, but after the search takes place, the list component's child reference to the search component doesn't work... 因此,在执行搜索之前,搜索组件的子引用有效,但是在搜索之后,列表组件对搜索组件的子引用不起作用...
BUMMER, this ended up being way less interesting than I was hoping for. 棒极了,这最终变得比我希望的有趣。 In my subscriptions to the searching requests from the user, I was using the finalize
operator on the observable that is the call to getUsers
, and was attempting to use a method group like I can in C#: 在订阅用户的搜索请求时,我在可观察对象(即对getUsers
的调用)上使用了finalize
运算符,并尝试使用在C#中可以使用的方法组:
this.userSearch.searches.subscribe((search: UserSearch) => {
this.loading = true;
const params = new UserSearchQueryParams();
params.Login = search.login;
params.UserName = search.user;
params.EnabledOnly = search.enabled;
this.users$ = this._users.getUsers(params).pipe(
catchError(this.handleError),
finalize(this.resetSearch)
);
});
If I change the usage of the finalize
operator to not include a method group, like so: 如果将finalize
运算符的用法更改为不包括方法组,如下所示:
...
finalize(() => this.resetSearch())
...
... then my this.userSearch
does not return undefined
in the body of resetSearch
. ...那么我的this.userSearch
不会在resetSearch
的正文中返回undefined
。 I've run into the issue of trying to use method groups in typescript before. 我之前遇到过尝试在打字稿中使用方法组的问题。 Sometimes they work, sometimes they don't, but this is a case of when they don't and I should just stop trying to use them I think... 有时它们起作用,有时却不起作用,但是这是当它们不起作用时的一种情况,我应该停止尝试使用它们,我认为...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.