相关疑难解决方法(0)

在角度2(Beta)中将一项服务注入另一项服务的最佳方法是什么?

我知道如何将服务注入组件(通过@Component),但是如何使用DI来传递组件之外的服务?

换句话说,我不想这样做:

export class MyFirstSvc {

}

export class MySecondSvc {
    constructor() {
        this.helpfulService = new MyFirstSvc();
    }
}

export class MyThirdSvc {
    constructor() {
        this.helpfulService = new MyFirstSvc();
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript angular

41
推荐指数
2
解决办法
4万
查看次数

使用另一个自定义服务的服务时,没有提供商...错误

所以,我们谈论的是angular2,我知道它仍处于alpha状态.我创建了一个我将在另一个服务中使用的基本服务.然后将后者用于组件中.

基本服务看起来像这样

import {Injectable, Observable} from 'angular2/core';
import {Http} from 'angular2/http';
import {RequestOptionsArgs, Connection, ConnectionBackend} from 'angular2/http';
import {BaseRequestOptions, RequestOptions} from 'angular2/http';
import {RequestMethods} from 'angular2/http';
import {Response} from 'angular2/http';
import {Request} from 'angular2/http';
import {makeTypeError} from 'angular2/src/facade/exceptions';

@Injectable()

export class MyOAuth extends Http {
  constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
    super(backend, defaultOptions);
  }

  /**
   * Performs any type of http request. First argument is required, and can either be a url or
   * a {@link Request} instance. If the first …
Run Code Online (Sandbox Code Playgroud)

ionic2 angular

5
推荐指数
1
解决办法
5785
查看次数

标签 统计

angular ×2

ionic2 ×1

javascript ×1