Angular 2将服务注入扩展类(BaseRequestOptions) [英] Angular 2 inject service into extended class(BaseRequestOptions)

查看:86
本文介绍了Angular 2将服务注入扩展类(BaseRequestOptions)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码扩展了BaseRequestOptions类:

I have the following code that extends the BaseRequestOptions class:

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

@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
    constructor(private serviceA:ServiceA) {       
        super();        
    }   
    merge(options?: RequestOptionsArgs): RequestOptions {        
        console.log(this.serviceA);
        //this.serviceA.myCustomMethod();
        return super.merge(options);
    }
}

当我引用this.serviceA时,值是undefined.当我将相同的服务注入其他服务/组件时,它会按预期工作.

When I reference this.serviceA the value is undefined. When I inject the same service into other services/components it is working as expected.

这是完整的引导程序代码:

Here is the complete bootstrap code:

import { Injectable } from '@angular/core';
import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS  } from './routes';
import { HTTP_PROVIDERS, BaseRequestOptions, RequestOptions, RequestOptionsArgs, Headers } from '@angular/http';
import { ServiceA } from './ServiceA';

@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
    constructor(private serviceA:ServiceA) {       
        super();        
    }   
    merge(options?: RequestOptionsArgs): RequestOptions {        
        console.log(this.serviceA);
        //this.serviceA.myCustomMethod();
        return super.merge(options);
    }
}

bootstrap(AppComponent, [
    APP_ROUTER_PROVIDERS,
    HTTP_PROVIDERS,  
    ServiceA,
    { provide: RequestOptions, useClass: AppRequestOptions }
]).catch(err => console.error(err));

ServiceA声明:

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

@Injectable()
export class ServiceA {
    myCustomMethod() {
     
    }
}

我正在使用Angular版本 2.0.0-rc.4

I am using Angular version 2.0.0-rc.4

我不确定是否是因为我正在扩展一个类并将其标记为@Injectable()?

I am not sure if it is because I am extending a class and marking it as @Injectable() ?

我已经检查过,唯一可以找到的相关问题是:

I have checked and the only related questions I can find are:

在内部注入服务Angular 2应用程序中的服务内部的服务

角度2:将服务注入到另一服务. (没有提供程序错误)

更新

似乎与一个打开的错误有关: https://github.com/angular/angular/issues/8925

Seems like it is related to a open bug : https://github.com/angular/angular/issues/8925

推荐答案

有一个打开的错误:尽管正确设置了用于依赖项注入的元数据,但未在构造函数级别将其提供给类实例.

Whereas the metadata for dependency injection is correctly set, the element isn't provided to the class instance at the constructor level.

@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
  constructor(private serviceA:ServiceA) {       
    super();        
    console.log(Reflect.getMetadata('design:paramtypes', AppRequestOptions));
    console.log(Reflect.getMetadata('parameters', AppRequestOptions));
  }

  (...)
}

有关更多详细信息,请参见此plunkr: https://plnkr.co/edit/hcqAfsI7u88jbjScq6m3?p =预览.

See this plunkr for more details: https://plnkr.co/edit/hcqAfsI7u88jbjScq6m3?p=preview.

修改

如果使用RequestOptions类而不是BaseRequestOptions一个,它将起作用:

It will work if you use the RequestOptions class instead of the BaseRequestOptions one:

@Injectable()
export class AppRequestOptions extends RequestOptions {
  constructor(private serviceA:ServiceA) {       
    super();        
  }   

  (...)
}

请参阅以下示例: https://plnkr.co/edit/laP04kqUCOR5qbFgo0iM?p=preview .

这篇关于Angular 2将服务注入扩展类(BaseRequestOptions)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆