简体   繁体   English

角度组件和助手服务或助手类

[英]angular component and helper service or helper class

i`m new to angular 5. 我是角度5的新手。

I`m trying to build the component that has lot of logic which prepares data for being able to be displayed in HTML. 我正在尝试构建具有很多逻辑的组件,该组件为可以在HTML中显示的数据做准备。

As far as i know - this logic should be moved from the component to some other place in order not to trigger change tracking.(as it says here in styleguide) 据我所知-这个逻辑应该从部件为了不触发更改跟踪移动到其他地方(因为它说, 这里的风格指南)

I this other place should be a servie or helper class? 我这个其他地方应该是服务班或助手班吗? If it service , should it be added to 'providers' of the component`s module of this component or to 'providers' of component itself? 如果提供服务,是否应将其添加到该组件的组件模块的“提供者”或组件本身的“提供者”?

@Component({
    selector: 'filters-bar',
    templateUrl: './filters-bar.component.html',
    styleUrls: ['./filters-bar.component.scss'],
    providers:[FiltersFromQueryService]
})

Thanks 谢谢

If that class is only used in one Component use it as normal class (helper class or as I call it View Object). 如果该类仅在一个组件中使用,则将其用作普通类(帮助类或我称为“查看对象”)。 You can't inject them in component but you can create new instance on every initialization: this.viewObject = new ViewObject(); 您不能将它们注入组件中,但是可以在每次初始化时创建新实例: this.viewObject = new ViewObject();

If it is used in multiple Components then it should be in Service so it can be injected in multiple components (initialized only once, singleton). 如果在多个组件中使用它,则应将其投入使用,以便可以将其注入多个组件中(仅初始化一次,单例)。

Providers are usualy singletone objects. 提供程序通常是单调对象。

When anotated with @Injectable(), this: 当用@Injectable()注释时,这是:

@Component({})
class Cmp {
  constructor() {
    this.yourService= new YourService(...);
  }
}

...can be writen like this: ...可以这样写:

@Component({
  ..
  providers: [YourService]
})

...or like this: ...或像这样:

@Component({})
class Cmp {
  constructor(private yourService: YourService ) {
  }
}

Singleton or not 是否单身

How many places you provide an injectable determines how many instances will be created (they are only instantiated if actually requested). 您提供多少个注入对象确定将创建多少个实例(仅在实际请求时实例化)。

If you want a single instance for your whole application, then provide an injectable only once at the root component (or with bootstrap(AppComponent, [...]) which results in the same behavior. 如果您要为整个应用程序使用一个实例,则在根组件处(或使用bootstrap(AppComponent,[...])仅提供一次可注入的操作,这将导致相同的行为。

If you want a new instance for each component A, then add it to providers of the component A 如果要为每个组件A提供一个新实例,则将其添加到组件A的提供程序中

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

相关问题 测试:角度的spyOn Helper类 - Testing: spyOn Helper class in angular Angular 2 Component Factory解析器帮助器功能 - Angular 2 Component Factory Resolver Helper Function Angular 6 Testing - 参考使用HttpClient的帮助程序的服务 - Angular 6 Testing - Service with reference to helper that uses HttpClient JWT Helper 服务在 Angular 10 中引发错误 - JWT Helper service throws error in Angular 10 从angular 6的帮助器类访问DOM元素 - Access a DOM element from a helper class angular 6 我们如何获得Helper类对组件的响应并在angular 7中与HTML绑定? - How we can get response from Helper class to component and bind with HTML in angular 7? 如何使用需要 TranslateService 的辅助方法测试 Angular 中的组件? - How do I test a component in Angular that with a helper method that requires TranslateService? Angular:翻译管道-视觉助手 - Angular: translation pipe - visual helper 如何动态注入辅助类 - How to inject helper class dynamically 如何创建一个Angular2 Component助手,该助手可以传递父组件中的项目并像在同一作用域中一样使用它们? - How do I create an Angular2 Component helper that can pass through items from a parent component and use them as if in the same scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM