Change default change detection strategy

2020-02-13 08:35发布

How to set the default change detection strategy to OnPush? Can it be set globally somehow?

I want to avoid having to adding this line to every component

@Component({
    ...
    changeDetection: ChangeDetectionStrategy.OnPush,
    ...
})

2条回答
做自己的国王
2楼-- · 2020-02-13 08:53

It is possible to set the change detection strategy to OnPush in the CLI so that newly generated components will have it set like that.

ng generate component test --changeDetection=OnPush

You can also set that value as default in your angular.json, so that you don't need to set the flag every time:

// angular.json
{
  //...
  "schematics": {
    "@schematics/angular": {
      "component": {
        "changeDetection": "OnPush"
      }
    }
  }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-13 09:07

The change detection strategy can only be defined per component or directive not globally.

Using a custom decorator is discouraged because it will not be supported by the upcoming offline template compiler.

查看更多
登录 后发表回答