Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No provider for... error when using service that uses another custom service

Tags:

angular

ionic2

So, we're talking about angular2 and I know it's still in alpha. I created a base service that I'm going to use in another service. The latter one is then used in a component.

Base service looks like this

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

The other service that uses the above one looks like this (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');
  }
}

Then in the class the uses this service i call it like this:

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

The error I get in the browser is

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

It doesn't seem right to me that I have to import MyOAuth as well...

like image 649
onizuka87 Avatar asked Oct 31 '22 14:10

onizuka87


1 Answers

When you need to inject custom services into other services, you need to provide the services in bigger context (component, page, app)

You can do:

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

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

For me, if services can be reuse in multiple places, I always define it in app context. Ex:

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

So those services will be instatiated only once. If you provide them in, for ex: Page1, Page2, they will be instatiated twice

like image 147
Hung Hoang Avatar answered Nov 08 '22 06:11

Hung Hoang