Typescript dependency injection public vs private

后端 未结 3 2060
遥遥无期
遥遥无期 2020-12-02 12:32

What is the difference between injecting service with public and private.I see most of examples use private keyword in angular component. Would it have any implications of u

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 12:52

    In addition to the prior answer ... anything marked as private cannot be accessed by the component's template either. (Private members can be accessed when using JIT, such as at development time, but not when using AOT, such as for production.)

    So in your template, you could only do *ngIf='carService.isValid' if the injected service was marked as public.

    But actually, best practice is to wrap any service properties/methods in a component property/method anyway and have the template bind to/call the component's property or method.

    Something like this:

       get isValid(): boolean {
          return this.carService.isValid;
       }
    

    And then access it like this: *ngIf='isValid'

提交回复
热议问题