当前位置:  开发笔记 > 前端 > 正文

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

如何解决《使用另一个自定义服务的服务时,没有提供商错误》经验,为你挑选了0个好方法。

所以,我们谈论的是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 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 {
    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;
  }
}

使用上述服务的其他服务看起来像这样(authenticate.js):

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');
  }
}

然后在课堂上使用这个服务我称之为:

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)

对我来说似乎也不对我进口MyOAuth...

推荐阅读
360691894_8a5c48
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有