简体   繁体   English

Angular中动态模板管理期间的ExpressionChangedAfterItHasBeenCheckedError

[英]ExpressionChangedAfterItHasBeenCheckedError during dynamic template management in Angular

I need to dynamically manage templates. 我需要动态管理模板。 I want to show or hide some view based on the parameter which is changed after WebSocket message or user interacition. 我想显示或隐藏一些基于WebSocket消息或用户交互后更改的参数的视图。 I use ngIf for this, but sometimes (especialy, when I reload the view, and messages comes quickly) console receive me an error 我为此使用ngIf,但是有时(尤其是当我重新加载视图时,消息很快出现)控制台收到一个错误

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false'.

I user Angular 8 我使用Angular 8

Main Component 主要成分

export class MainComponent implements AfterViewInit, OnInit {

 public view_config;

 constructor(public pluginInfoService: PluginInfoService){}

 ngOnInit() {
   this.pluginInfoService.getViewConfig().subscribe(view_config => {
     this.view_config = view_config;
   });
 }

}

Main Component Template 主要组件模板

<popup-window *ngIf="view_config.show_popup_window" class="overflow_hidden h-100 w-100" [view_config]="view_config">
  <select-media *ngIf="view_config.popup_window_mode == 'select_media'" [view_config]="view_config" class="container-fluid h-100"></select-media>
  <get-contact-name *ngIf="view_config.popup_window_mode == 'get_contact_name'" [view_config]="view_config" class="container-fluid h-100"></get-contact-name>
  <chat-view *ngIf="view_config.popup_window_mode == 'chat_view'" class="container-fluid h-100 flex-column d-flex p-0"></chat-view>
  <change-media-invitation *ngIf="view_config.popup_window_mode == 'media_change_invite'" [view_config]="view_config" class="container-fluid h-100"></change-media-invitation>
</popup-window>

Plugin Info Servie 插件信息服务

export class PluginInfoService {

  private view_config = {
    show_main_button: true,
    show_popup_window: true,
    popup_window_mode: 'select_media',
    show_popup_media_window: false,
    popup_media_window_mode: null,
    minimize_popup_window: false
  };

  private viewConfigStream = new BehaviorSubject(this.view_config);

  constructor() {
  }

  setViewConfigParam(param, value) {
    this.view_config[param] = value;
    this.viewConfigStream.next(this.view_config);
  }

  getViewConfig(): Observable<any> {
    return this.viewConfigStream.asObservable();
  }
}

The mechanism already works, but I suppose it makes more problems around it. 该机制已经起作用,但我想它会带来更多问题。 Firstly I don't want have this errors, secondly I want to be sure that all is implemented properly. 首先,我不想出现此错误,其次,我想确保所有操作均正确执行。 How to fix this problem ? 如何解决这个问题? Is this method is good ? 这种方法好吗?

If you want to avoid possible changes to the views whilst the lifecycle is still in the process of generating the page you could simply hook into a later point, eg 如果要避免在生命周期仍在生成页面的过程中对视图进行可能的更改,则可以简单地挂接到后面的位置,例如

replace 更换

ngOnInit()

with

ngAfterViewInit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角动态组件和ExpressionChangedAfterItHasBeenCheckedError - Angular Dynamic Components and ExpressionChangedAfterItHasBeenCheckedError Angular动态组件加载 - ExpressionChangedAfterItHasBeenCheckedError - Angular dynamic component loading - ExpressionChangedAfterItHasBeenCheckedError 动态组件中的 Angular ExpressionChangedAfterItHasBeenCheckedError - Angular ExpressionChangedAfterItHasBeenCheckedError inside Dynamic Components Angular 4动态页面加载器:ExpressionChangedAfterItHasBeenCheckedError - Angular 4 dynamic page loader : ExpressionChangedAfterItHasBeenCheckedError ExpressionChangedAfterItHasBeenCheckedError:角度 - ExpressionChangedAfterItHasBeenCheckedError: angular ExpressionChangedAfterItHasBeenCheckedError Angular - ExpressionChangedAfterItHasBeenCheckedError Angular Angular 4 ExpressionChangedAfterItHasBeenCheckedError - Angular 4 ExpressionChangedAfterItHasBeenCheckedError 反应形式动态表单验证中的 angular 6“ExpressionChangedAfterItHasBeenCheckedError”添加和删除 - angular 6 "ExpressionChangedAfterItHasBeenCheckedError" in reactive form dynamic form validation add and remove 带有动态清晰度选项卡的 Angular 表单在添加选项卡后获取 ExpressionChangedAfterItHasBeenCheckedError - Angular form with dynamic clarity tabs get ExpressionChangedAfterItHasBeenCheckedError after add tab ExpressionChangedAfterItHasBeenCheckedError /角4角7 - ExpressionChangedAfterItHasBeenCheckedError / Angular 4-Angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM