在Angular中,可以通过使用ChangeDetectionStrategy来将变更检测限制在当前组件及其子组件中。
changeDetection属性为ChangeDetectionStrategy.OnPush,以告诉Angular该组件使用的是OnPush的变更检测策略。@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponentComponent {
  // 组件的代码
}
ChangeDetectorRef来手动触发变更检测。import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
@Component({
  selector: 'app-parent-component',
  templateUrl: './parent-component.component.html',
  styleUrls: ['./parent-component.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ParentComponentComponent {
  constructor(private cdr: ChangeDetectorRef) {}
  // 手动触发变更检测
  detectChanges() {
    this.cdr.detectChanges();
  }
}
detectChanges方法。
这样,当父组件调用detectChanges方法时,只有当前组件及其子组件会进行变更检测,其他组件不会被检测到。