简体   繁体   English

在Angular5 =“不是函数”中提供服务

[英]Provide a service in Angular5 = “is not a function”

I want to override a service from an other module, but i got the error "is not a function" 我想覆盖其他模块中的服务,但出现错误“不是函数”

In my component (module 1) i inject the servie 在我的组件(模块1)中,我注入了服务

public constructor(private chartProgressService: ChartProgressService) {
}

In module 2 i override the servive in providers 在模块2中,我覆盖提供程序中的服务

providers: [
    {
        provide: Configuration,
        useClass: AppConfiguration,
    },
    {
        provide: ChartProgressService,
        useValue: MyChartProgressService
    },
    {
        provide: LOCALE_ID,
        useValue: 'de-DE',
    }
],

and this is MyChartProgressService 这是MyChartProgressService

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

@Injectable()
export class InnogyChartProgressService {

    public getUnit(): string {
        return '';
    }

    public getValue(currentValue: number, maxValue: number): number {
        return currentValue;
    }
}

The call this.chartProgressService.getValue() in my component returns the error 在我的组件中调用this.chartProgressService.getValue()返回错误

HeaderComponent.html:11 ERROR TypeError: this.chartProgressService.getUnit is not a function
at ChartProgressComponent.ngOnInit (chart-progress.component.ts:33)
at checkAndUpdateDirectiveInline (core.js:12369)
at checkAndUpdateNodeInline (core.js:13893)
at checkAndUpdateNode (core.js:13836)
at debugCheckAndUpdateNode (core.js:14729)
at debugCheckDirectivesFn (core.js:14670)
at Object.eval [as updateDirectives] (HeaderComponent.html:11)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:14655)
at checkAndUpdateView (core.js:13802)
at callViewAction (core.js:14153)

I think i need your help! 我想我需要你的帮助! Thanks! 谢谢!

one more thing is if you want to use InnogyChartProgressService 还有一件事是,如果您想使用InnogyChartProgressService

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

@Injectable()
export class InnogyChartProgressService {
}

then it should be like 那应该像

 {
    provide: ChartProgressService,
    useClass: InnogyChartProgressService 
},

in you case you are referring different class called MyChartProgressService and change useClass 在这种情况下,您要引用另一个名为MyChartProgressService类并更改useClass


As per angular guide if you want to replace service with new service than you need to extend service , for example given in angular guide 根据角度指南,如果您要用新服务替换服务,而不是需要扩展服务,例如角度指南

to do this , replace old Logger with new one 为此,请用新的Logger替换旧的Logger

[{ provide: Logger, useClass: BetterLogger }]

it extends old one in new one as below 它将旧的扩展为新的,如下所示

@Injectable()
export class EvenBetterLogger extends Logger {
}

Read : https://angular.io/guide/dependency-injection#alternative-class-providers 阅读: https : //angular.io/guide/dependency-injection#alternative-class-providers

The problem actually was the following: If you use useValue in your providers definitions, you have to use an actual instance there like new MyChartProgressService() or object literal. 问题实际上是以下原因:如果在提供程序定义中使用useValue ,则必须在那里使用实际实例,例如new MyChartProgressService()或对象文字。 But if you want to just give a "blueprint" and let it be instantiated automatically by Angular, use useClass with a type, so in your case useClass: MyChartProgressService . 但是,如果您只想给出一个“蓝图”并由Angular自动实例化,请使用useClass和一个类型,因此在您的情况下,请使用useClass: MyChartProgressService

More explanation can be found in this question . 在这个问题中可以找到更多的解释。

{提供:ChartProgressService, useClass :MyChartProgressService}

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

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