[英]Angular ViewChild Not Working When I Use Observables
I am using Angular 10
, and also NGXS 3.7.0
as my state management framework in my web application.我在我的 web 应用程序中使用
Angular 10
和NGXS 3.7.0
作为我的 state 管理框架。
There are two components in my application;我的应用程序中有两个组件; let's say
ParentComponent
and ChildComponent
.假设
ParentComponent
和ChildComponent
。 The ChildComponent
has a field called resources
which is an array of ResourceInterface
that by default equals to an empty array (ie, []
). ChildComponent
有一个名为resources
的字段,它是一个ResourceInterface
数组,默认情况下等于一个空数组(即[]
)。 Inside the ChildComponent
there is also a Run
button, which emits an EventEmitter to inform the ParentComponent
that the end user has clicked the Run
button;在
ChildComponent
内部还有一个Run
按钮,它发出一个 EventEmitter 来通知ParentComponent
最终用户点击了Run
按钮; hence, hey father, fetch the resources from the server and then update my resources
field.因此,嘿,父亲,从服务器获取资源,然后更新我的
resources
字段。
The problem is that, in the ParentComponent
, when I don't use Observables it works, but when I subscribe to observable then it does not work.问题是,在
ParentComponent
中,当我不使用 Observables 时它可以工作,但是当我订阅 observable 时它就不起作用。 In fact, I have to use observable and cannot do my stuff without observable.事实上,我必须使用 observable 并且没有 observable 就无法做我的事情。 In the following section I have provided the codes.
在下一节中,我提供了代码。
Child Component
: Child Component
:
@Component({
selector: '...',
templateUrl: '...',
styleUrls: ['...'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChildComponent implements OnInit {
resources: ResourceInterface[] = [];
@Output() runRequest: EventEmitter<void>;
constructor() {
this.runRequest = new EventEmitter<void>();
}
onRunRequest(): void {
this.runRequest.emit();
}
}
Parent Component
: Parent Component
:
@Component({
selector: '...',
templateUrl: '...',
styleUrls: ['...'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ParentComponent implements OnInit {
rId: string;
request$: Observable<DURequest | undefined>;
@ViewChild(ChildComponent) child!: ChildComponent;
constructor(
private _service: DURService,
private _rService: RService,
private route: ActivatedRoute,
) {
this.rId = this.route.snapshot.paramMap.get('id')!;
this.request$ = this.rId ? this._service.watchDUR(this.rId) : of(undefined);
}
onRunRequest(): void {
// This block works
this.request$.subscribe(req => {
this.child.resources = [
{
Id: '123',
fullUrl: 'some full url'
}
];
});
// This block does NOT work
// this.request$.subscribe(req => {
// this._service.runRequest(req!).subscribe(res => {
// this.child.resources = [
// {
// Id: '123',
// fullUrl: 'some full url'
// }
// ];
// });
// });
// This block does NOT work
// this.request$.subscribe(req => {
// this._service.runRequest(req!).subscribe(res => {
// this._service.fetchResources().subscribe(resources => {
// this.child.resources = [
// {
// Id: '123',
// fullUrl: 'some full url'
// }
// ];
// });
// });
// });
// The actual code should be this one, but this one does NOT work, too
// this.request$.subscribe(req => {
// this._service.runRequest(req!).subscribe(res => {
// this._service.fetchResources().subscribe(resources => {
// this.child.resources = resources;
// });
// });
// });
}
}
@ViewChild(Component, { static: true} @ViewChild(组件,{ static:真}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.