0

我想重新调用我的 ngAfterViewInit,因为它负责改变我的 dom 的样式。因此,如果我的组件数据发生更改,我的组件会更新,但我的样式不会应用于新数据。在下面的代码中,我负责显示一些产品信息的组件订阅了可观察的路由器参数。因此,如果我更改路由参数,则信息会在不重新运行我的组件的情况下更改,但不会应用样式,因为不会再次启动组件,因此不会再次调用 afterViewInit。这是我的代码

ngOnInit() {
    this.currentRoute.params.subscribe((params) => {
      this.productId = params['productId'];
      this.category = params['category'];
      this.productService.getSingleProduct(this.category, this.productId); //fetching product data
    });
  }
ngAfterViewInit(){
   // some codes to change the dom styles
}

那么,如果我获得新的产品数据集,如何重新调用我的 ngAfterViewInit 呢?

**重要点----

1- 我不能将我的 ngAfterViewInit 代码放入 params 订阅,因为该代码取决于动态内容。

2-我知道我可以在 ngAfterViewChecked 生命周期钩子中调用 ngAfterViewInit 方法,该方法工作得很好,但我认为我经常调用这些函数。

4

0 回答 0