简体   繁体   English

Angular 将提供程序添加到组件的最佳实践是什么

[英]Angular what is the best practice for adding providers to component

I have built an Angular application which uses lazy loading.我构建了一个使用延迟加载的 Angular 应用程序。 Throughout the application if there is a 1:1 relationship between a component and service I have structured my code as below:在整个应用程序中,如果组件和服务之间存在 1:1 的关系,我的代码结构如下:

@Injectable()
export class ContactFormService {
    ....stuff...
}

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss'],
  providers: [ ContactFormService ]
})
export class ContactComponent {
    ....stuff...
}

As you can see I've directly added the service as one of the providers, rather than using @Injectable({ providedIn: 'root' }) .如您所见,我直接将服务添加为提供者之一,而不是使用@Injectable({ providedIn: 'root' })

At the time I thought it would help any potential issues around using a singleton, should this service be used in a different component and lose that 1:1 relationship.当时我认为这将有助于解决使用 singleton 的任何潜在问题,如果该服务用于不同的组件并失去 1:1 的关系。

The side effect that I have noticed is that when writing unit tests I have to add overrideComponent() to the configuration我注意到的副作用是,在编写单元测试时,我必须将overrideComponent()添加到配置中

I have to do this (NOTE: I have to override the provided service):我必须这样做(注意:我必须覆盖提供的服务):

TestBed.configureTestingModule({
   declarations: [ContactComponent]
})
.overrideComponent(ContactComponent, 
   { set: { providers: [{ provide: ContactFormService, useValue: contactFormServiceSpyObj }] }})
.compileComponents();

Instead of this:而不是这个:

await TestBed.configureTestingModule({
  providers: [
    { provide: ContactFormService, useValue: contactFormServiceSpyObj }
  ],
  declarations: [ ContactComponent ]
}).compileComponents();

This has made me wonder am I following a really bad practice?这让我想知道我是否遵循了一个非常糟糕的做法? and if so, what circumstances would be suitable to inject a service in the way that I am doing so?如果是这样,什么情况适合以我这样做的方式注入服务?

I understand that @Injectable({ providedIn: 'root' }) helps with the tress shaking process when building the application我知道@Injectable({ providedIn: 'root' })在构建应用程序时有助于 tress shaking 过程

I see nothing wrong with what you're doing - I also add services to component providers when I want them to be exclusive to the component instance (and possibly the children).我看不出你在做什么 - 当我希望它们专用于组件实例(可能还有子实例)时,我也会向组件提供者添加服务。

You should remember though to clean up resources on ngOnDestroy method in the service .你应该记得在服务中使用ngOnDestroy方法清理资源。 (Yes, angular calls ngOnDestroy on services). (是的,angular 在服务上调用ngOnDestroy )。

In situations where only one component instance is displayed at once I prefer a different approach to component provider.在一次只显示一个组件实例的情况下,我更喜欢使用不同的方法来提供组件。 I use a singleton service provided in root and call an initialization method of the service on the component ngOnInit .我使用 root 中提供的 singleton 服务,并在组件ngOnInit上调用该服务的初始化方法。 In that method I clean up any resources left over from previous component instance.在该方法中,我清理了先前组件实例遗留下来的所有资源。 That comes really handy when I'm using a single application wide state tree and I don't want to generate keys for each component separately.当我使用单个应用程序范围的 state 树并且我不想分别为每个组件生成密钥时,这真的很方便。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM