00009.为什么插值表达式多次执行

2023-10-11  本文已影响0人  笑着字太黑

【Angular】中的变化检测(ChangeDetection)_angular changedetection-CSDN博客

This is because you are using the Default change detection strategy on your component. By default all components use this strategy which means that when Angular determines a component's state is dirty it re-renders the template and cause the testnDisplay function to be called. In order to remove the component from default checking you should set the strategy to OnPush which is much more perfomant because it only re-renders the template when one of the @Input properties changes. It is still possible to have the template re-rendered but it requires the component to tell angular when to do so. Example:

import { ChangeDetectionStrategy, Component, ViewEncapsulation, OnInit, AfterViewInit, OnDestroy, HostListener } from '@angular/core';

@Component({
  /* ... */
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppLogoComponent  {
  testnDisplay(type){
    console.log(type);
  }
}
上一篇 下一篇

猜你喜欢

热点阅读