简体   繁体   English

带有 OnPush 更改检测的角度更新属性

[英]Angular update property with OnPush change detection

With this code using changeDetection: ChangeDetectionStrategy.Default使用此代码使用 changeDetection:ChangeDetectionStrategy.Default

export class HomePageComponent {
  constructor(
    private i18nService: I18nService
  ) {}
  get language(): string {
    return this.i18nService.language;
  }
}

angular update the view but using changeDetection: ChangeDetectionStrategy.OnPush,角度更新视图,但使用 changeDetection:ChangeDetectionStrategy.OnPush,

it doesn't so the question is:事实并非如此,问题是:

How can update the view using OnPush?如何使用 OnPush 更新视图?

OnPush will only run change detection once (on creation). OnPush 只会运行一次更改检测(在创建时)。 And Angular then only triggers change detection when a reference of an @Input() property changes (or if an Event handler of your component gets invoked).然后,Angular 仅在 @Input() 属性的引用发生更改时(或者如果您的组件的事件处理程序被调用)才会触发更改检测。 In your component I see no @Input() statements at all.在您的组件中,我根本看不到 @Input() 语句。

You can manually run change detection by injecting ChangeDetectorRef into your component and calling appropriate methods on it.您可以通过将 ChangeDetectorRef 注入组件并在其上调用适当的方法来手动运行更改检测。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM