2015-12-04 9 views
5

これで、angular2について話しています。 別のサービスで使用する基本サービスを作成しました。後者はコンポーネント内で使用されます。別のカスタムサービスを使用しているサービスを使用しているときにプロバイダが...エラーなし

import {Injectable, Inject} from 'angular2/angular2'; 
import {MyOAuth} from './MyOAuth'; 


@Injectable() 

export class AuthenticationService { 

    constructor(@Inject(MyOAuth) http) { 
    this.http = http; 
    } 

    authenticate(username, password, community) { 
    console.log('authenticate'); 
    } 
} 

そしてクラスでの使用このサービス:

基本サービスは、上記の1つを使用する他のサービスが、この(authenticate.js)のように見えるこの

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 argument is a url, an optional {@link RequestOptions} 
    * object can be provided as the 2nd argument. The options object will be merged with the values 
    * of {@link BaseRequestOptions} before performing the request. 
    */ 
    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { 
    var responseObservable: any; 
    if (isString(url)) { 
     responseObservable = httpRequest(
      this._backend, 
      new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url))); 
    } else if (url instanceof Request) { 
     responseObservable = httpRequest(this._backend, url); 
    } else { 
     throw makeTypeError('First argument must be a url string or Request instance.'); 
    } 
    return responseObservable; 
    } 
} 

ように見えます私は次のように呼ぶ:

import {Page} from 'ionic/ionic'; 
import './login.scss'; 
import {AuthenticationService} from '../../services/authentication'; 

@Page({ 
    templateUrl: 'app/login/login.html', 
    providers: [AuthenticationService] //As of alpha 42 
}) 

私は、ブラウザで取得エラーが

EXCEPTION: No provider for MyOAuth! (LoginPage -> AuthenticationService -> MyOAuth) 

であるあなたが同様にあなたのサービスを提供する必要が

+0

私はこの[問題](https://github.com/angular/angular/issues/5155)が参考になると思います。 –

答えて

2

あなたが他のサービスへのカスタムサービスを注入する必要がある場合、あなたはより大きな文脈(コンポーネント、ページ、アプリ)でサービスを提供する必要が

import {Page} from 'ionic/ionic'; 
import './login.scss'; 
import {AuthenticationService} from '../../services/authentication'; 

@Page({ 
    templateUrl: 'app/login/login.html', 
    providers: [[AuthenticationService], [MyOAuth]] 
}) 

私にとって、サービスは複数の場所で再利用できる場合は、常にアプリケーションのコンテキストで定義します。例:

@Component({ 
    templateUrl: 'build/app.html', 
    providers: [[AuthenticationService], [MyOAuth]] 
}) 
class MyApp{ 
} 

これらのサービスは、一度だけ開始されます。あなたがそれらを提供する場合、例:Page1、Page2、それらは2回instathiatedされます

0

...私もMyOAuthをインポートする必要があることに私に権利いないようです。

あなたが行うことができます
import {Page} from 'ionic/ionic'; 
import {provider} from '@angular/core'; 
import './login.scss'; 
import {AuthenticationService} from '../../services/authentication'; 

@Page({ 
    templateUrl: 'app/login/login.html', 
    providers: [AuthenticationService, provide(MyOAuth, {useClass: MyOAuth})] 
}) 
関連する問題