Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angular - Change default change detection strategy

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,
    ...
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...