Typescript 依赖注入公共 vs 私有

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46596399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:56:54  来源:igfitidea点击:

Typescript dependency injection public vs private

angulartypescript

提问by Vimal Trivedi

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 using public? e.g.

使用 public 和 private 注入服务有什么区别。我看到大多数示例在 angular 组件中使用 private 关键字。使用 public 会有什么影响吗?例如

constructor(public carService: CarService) { }

vs.

对比

constructor(private carService: CarService) { }

回答by DeborahK

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

除了先前的答案......组件的模板也无法访问任何标记为私有的内容。(私有成员使用JIT时,例如在开发时未使用时AOT,诸如用于生产访问,但是。)

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

所以在你的模板中,你只能*ngIf='carService.isValid'在注入的服务被标记为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'

然后像这样访问它: *ngIf='isValid'

回答by Commercial Suicide

The answer is pretty simple: you have to create private variables when you don't need to use them outside of current class/component, otherwise, you should create public variables. And one more thing: you can also use private variables and give access to them from outside via special functions called gettersand setters. For example:

答案非常简单:当您不需要在当前类/组件之外使用它们时,您必须创建私有变量,否则,您应该创建公共变量。还有一件事:您还可以使用私有变量,并通过称为gettersetter 的特殊函数从外部访问它们。例如:

private _customValue: any;

set customValue(newValue: any): void {
  this._customValue = newValue;
}

get customValue(): any {
  return this._customValue;
}

Notice, that _customValueis private, but you can set/get this value from outside the class via operations with customValue:

请注意,这_customValue是私有的,但您可以通过以下操作从类外部设置/获取此值customValue

classInstance.customValue = 'newValue';
console.log(classInstance.customValue);

Need to say, that setand getkeywords before method names are not strongly needed, they are more for clarification.

需要说的是,方法名前的关键字setget关键字并不是非常需要,它们更多是为了澄清。

回答by apaternina

For cases when you have a service for example:

例如,对于您有服务的情况:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})

export class CarService {

  constructor() { }

  public design = {
    "color": "blue"
  }
}

And in your constructor where your are going to implement the service

在您将要实现服务的构造函数中

constructor(private carService: CarService) { }

You can use a normal method for return the service

您可以使用正常方法返回服务

 getCarService() {
      return this.carService;
 }

And in your template you can do

在你的模板中你可以做

<div>{{getCarService().design.color}}</div>