gpt4 book ai didi

angular - 如何使用@Injectable providedIn root in angular 5 with getting

转载 作者:太空狗 更新时间:2023-10-29 18:23:10 28 4
gpt4 key购买 nike

当我使用

@Injectable({
providedIn: 'root'
})

消息 Expects 0 arguments but got 1. Any idea on what to do. 消息 Expects 0 arguments but got 1. Any idea on what to do.

我一直在关注这里的文档 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);
}
}

最佳答案

我认为此功能是在 Angular 6 中引入的,因此您必须删除它并改为在组件的模块中提供服务:

@NgModule
....
providers: [MyService]

关于angular - 如何使用@Injectable providedIn root in angular 5 with getting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51542627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com