简体   繁体   English

Angular 2 将复杂的服务注入到服务中

[英]Angular 2 Inject complex service to service

I have three services and one component:我有三个服务和一个组件:

  • root.service
  • abstarct-child.service
  • extend-child.service
  • app.component

The root.service injects by using dependency injection the abstarct-child.service , like so: root.service使用依赖注入注入abstarct-child.service ,如下所示:

import {Injectable} from 'angular2/core';
import {AbstractChildService} from './abstarct-child.service';

@Injectable()

export class RootService {
    constructor(private _abstractChildService: AbstractChildService) {}
    UseChildServiceDoSomethingFunction(){}  
}

The abstarct-child.service looks like so: abstarct-child.service看起来像这样:

import {Injectable} from 'angular2/core';

@Injectable()
export abstract class AbstractChildService {
    abstract doSomething(): any ;
}

The extend-child.service looks like so: extend-child.service看起来像这样:

import {Injectable} from 'angular2/core';
import {AbstractChildService} from './abstarct-child.service';

@Injectable()
export class ExtendChildService extends AbstractChildService{

    constructor(private _num: number) { super(); }
    doSomething() : any { return true; }
}

The app.component provides the root.service and the extend-child.service as the abstract-child.service like so: app.component提供root.serviceextend-child.service作为abstract-child.service像这样:

import {Component} from 'angular2/core';
import {Input} from 'angular2/core';
import {provide} from 'angular2/core';
import {RootService} from './root.service';
import {AbstractChildService} from './abstarct-child.service';
import {ExtendChildService} from './extend-child.service';

@Component({
    selector: 'app',
    providers: [provide(AbstractChildService, { useClass: ExtendChildService, useValue: this.num})]
})
export class AppComponent {
    @Input('given_Num') num: number;
}

I would like to know how can I inject to the extend-child.service an object, in this case the this.num of app.component ?我想知道如何向extend-child.service注入一个对象,在这种情况下是this.numapp.component

Can anyone find what I'm doing wrong?谁能找到我做错了什么?

Thanks.谢谢。

I haven't yet fully figured out what you try to accomplish but我还没有完全弄清楚你想完成什么,但是

   provide(AbstractChildService, { 
       useClass: ExtendChildService, 
       useValue: this.num})

is not a valid provider.不是有效的提供者。

You can't have an useClass and useValue parameter in one provider, only one at a time.一个提供程序中不能有useClassuseValue参数,一次只能有一个。

You also can't use this.你也不能使用this. in a provider in a @Component() decorator.@Component()装饰器中的提供者中。 When the decorator is evaluated, there is not instance of the component yet where this.当装饰器被评估时,这里还没有组件的实例this. could refer to.可以参考。

If you want to provide a number you can use a string or an OpaqueToken如果你想提供一个数字,你可以使用字符串或OpaqueToken

provide('MyNumber', {useValue: 10}),

and get it injected by并将其注入

constructor(@Inject('MyNumber') private _num: number) { super(); }

another way is to use a factory另一种方法是使用工厂

provide(ExtendedChildService, {useFactory: () => { new ExtendedChildService(10)});

but this.但是this. still can't be referred.仍然无法参考。

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

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