简体   繁体   English

如何使用带角度5的@Injectable providedIn根获取

[英]How to use the @Injectable providedIn root in angular 5 with getting

When I use the 我用的时候

@Injectable({
    providedIn: 'root'
})

The lines keep getting underlined in red with the message Expects 0 arguments but got 1. Any ideas on what to do. 这些行以红色加下划线,显示消息Expects 0参数但得到1.有关该怎么做的任何想法。

I've been following the documentation here https://angular.io/tutorial/toh-pt4#injectable-services Here is the code: 我一直在关注这里的文档https://angular.io/tutorial/toh-pt4#injectable-services这是代码:

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { MessageService } from './message.service';

@Injectable({
  providedIn: 'root',
})
export class HeroService {

//Modify the constructor with a parameter that declares a private 
messageService property. 
 // Angular will inject the singleton MessageService into that property when 
it creates 
  // the HeroService.
 constructor(private messageService: MessageService) { }

getHeroes(): Observable<Hero[]> {
// TODO: send the message _after_ fetching the heroes
this.messageService.add('HeroService: fetched heroes');
return of(HEROES);
}
}

I think this feature was introduced in Angular 6, so you'll have to remove that and instead provide the service in your component's module: 我认为这个功能是在Angular 6中引入的,所以你必须删除它,而是在组件的模块中提供服务:

@NgModule
....
providers: [MyService]

暂无
暂无

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

相关问题 @Injectable(providedIn: &#39;root&#39;) 的语义? - The semantics of @Injectable(providedIn: 'root')? 为什么我们使用@Injectable({ providedIn: 'root' }) 来达到 class? - Why we use @Injectable({ providedIn: 'root' }) to reach class? Angular 6提供了 - 如何自定义@Injectable()提供程序以进行依赖注入? - Angular 6 providedIn - how to customize the @Injectable() provider for dependency injection? 提供Angular 7服务:&#39;root&#39; - Angular 7 Service providedIn: 'root' 尽管使用了 @Injectable({providedIn: &#39;root&#39; }) 装饰器,Angular 服务在注入到组件中时不会充当单例 - Angular Service not acting as singleton when injected in components despite using @Injectable({providedIn: 'root' }) decorator Angular 7/8:Treeshakable 提供程序(提供在:'root') - Angular 7/8: Treeshakable providers (providedIn: 'root') 在“root”中提供的 angular 服务未定义 - providedin 'root' angular service is undefined Angular 提供了根和根模式 - Angular providedIn root and forRoot pattern 即使@Injectable 提供了provideIn: &#39;root&#39;,服务也会创建另一个实例。 如何确保服务是单身人士? - Service creates another instance even though @Injectable has providedIn: 'root'. How can I make sure the service is a Singleton? 是否使用@Injectable providedIn为任何非延迟加载的模块与提供的相同:“root”? - Is using @Injectable providedIn for any non-lazy-loaded module the same as providedIn: “root”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM