简体   繁体   English

Angular 当我使用 Observables 时 ViewChild 不工作

[英]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 10NGXS 3.7.0作为我的 state 管理框架。

There are two components in my application;我的应用程序中有两个组件; let's say ParentComponent and ChildComponent .假设ParentComponentChildComponent 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.

相关问题 Angular 6 @Viewchild不支持延迟加载 - Angular 6 @Viewchild is not working with lazy loading Angular 2 * ngFor和form提交移至可观察对象时停止工作 - Angular 2 *ngFor and form submit ceased working when moved to observables 如何比较可观察对象:NGRX和使用可观察对象的可观察对象进行有效的Angular 4渲染 - How to compare Observables: NGRX and Working with Observables of Observables for efficient Angular 4 Rendering angular4 viewchild没有在dom元素上工作 - angular4 viewchild not working on dom element 无法读取未定义的属性 @ViewChild 不工作 Angular 5 - Cannot read property of undefined @ViewChild not working Angular 5 将 ViewChild 用于动态元素 - Angular 2 &amp; ionic 2 - Use ViewChild for dynamic elements - Angular 2 & ionic 2 如何在Angular 2+中将ViewChild用于动态创建的html元素? - How do I use ViewChild for dynamic created html elements in Angular 2+? 实例化几次@ViewChild,以便我可以再次使用HTML Angular 4 - Instantiate several times a @ViewChild so i can use the HTML again Angular 4 如何将 @ViewChild 与外部 ng 模板一起使用(Angular 11) - How do I use @ViewChild with an external ng-template (Angular 11) Observables - Angular - .map 不工作,但 .subscribe 可以 - Observables - Angular - .map is not working but .subscribe does
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM