Shamsher
Shamsher

Reputation: 1761

Angular 2 Error: Invalid provider - only instances of Provider and Type are allowed, got: [object Object]

I am integrating ui-router2 with angular2-rc-4 but i am getting

Error: Invalid provider - only instances of Provider and Type are allowed, got: [object Object]

Following is the bootstraping code


    import {trace, UIROUTER_PROVIDERS, UIView, UIRouterConfig, Category, UIROUTER_DIRECTIVES} from "ui-router-ng2";
    import {HTTP_PROVIDERS} from "@angular/http";
    import {provide, PLATFORM_DIRECTIVES} from "@angular/core";
    import {LocationStrategy, HashLocationStrategy, PathLocationStrategy, PlatformLocation} from "@angular/common";
    import {BrowserPlatformLocation} from '@angular/platform-browser';
    import 'rxjs/add/operator/toPromise';
    import 'rxjs/add/operator/map';

    import { APP_BASE_HREF } from '@angular/common';
    import { disableDeprecatedForms, provideForms } from '@angular/forms';
    import { enableProdMode } from '@angular/core';
    import { bootstrap } from '@angular/platform-browser-dynamic';

    import { InitialStates } from './app.routes';
    import { AppComponent } from './app.component';
    import {MyUIRouterConfig} from "./_bootstrap/router.config";

    if ('' === 'prod') { enableProdMode(); }

    trace.enable(Category.TRANSITION, Category.VIEWCONFIG);

    bootstrap(UIView, [
        disableDeprecatedForms(),
        provideForms(),
        InitialStates,
        {
          provide: APP_BASE_HREF,
          useValue: ''
         },


         provide(LocationStrategy, { useClass: HashLocationStrategy }),

         provide(LocationStrategy, { useClass: PathLocationStrategy }),
         provide(PlatformLocation, { useClass: BrowserPlatformLocation }),

        ...UIROUTER_PROVIDERS,
        ...HTTP_PROVIDERS,

        provide(UIRouterConfig, { useClass: MyUIRouterConfig }),

        provide(PLATFORM_DIRECTIVES, {useValue: [UIROUTER_DIRECTIVES], multi: true})
    ]);

Following is my router config.

import {UIRouter} from "ui-router-ng2";
import {InitialStates} from "../app.routes";
import {Injectable, Injector} from "@angular/core";

@Injectable()
export class MyUIRouterConfig {
  constructor(private injector: Injector) {}

  configure(uiRouter: UIRouter) {
    // Register each state definition (from app.states.ts) with the StateRegistry
    InitialStates.forEach(state => uiRouter.stateRegistry.register(state));

    // Define a default behavior, for when the URL matched no routes
    uiRouter.urlRouterProvider.otherwise(() => uiRouter.stateService.go("app", null, null) && null);
  }
}

Upvotes: 21

Views: 34578

Answers (4)

O-9
O-9

Reputation: 1779

This answer is not to the original one, but the error message is the same.

I got this error because I had in my test file (app.component.spec.ts):

 describe('AppComponent', () => {

  let translate: TranslateService; // don't use in providers array!

  beforeEach(async() => {
    await TestBed.configureTestingModule({
      imports: [
        // ...
      ]
      // ...
      providers: [
        traslate,
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
    }).compileComponents();
  });

When the correct way of defining provider is

providers: [TranslateService],

Upvotes: 0

György Balássy
György Balássy

Reputation: 2998

The same error is thrown for a simple typo, if you write this:

{ provider: MyService, useValue: myServiceMock }

Instead of this:

{ provide: MyService, useValue: myServiceMock }

Note the difference between provider and provide. The correct is provide.

Hope this helps.

Upvotes: 49

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658027

Sounds like you are using an Angular2 version where this kind of provider is not yet supported

{
  provide: APP_BASE_HREF,
  useValue: '<%= APP_BASE %>'
 },

AFAIR this was introduced in RC.4 or RC.3

Please try instead

@NgModule({
  ..., 
  { provide: APP_BASE_HREF, useValue: '<%= APP_BASE %>'
})
export class AppModule {}

Upvotes: 2

Shamsher
Shamsher

Reputation: 1761

The issue got resolved. The problem was ui-router-ng2 documentation was not clear if you want to integrate ui-router-ng2 in your existing application. I have bootsraped AppComponent instead of UIView which was incorrect in documentation. Following is the code.

import { enableProdMode, provide } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { LocationStrategy, HashLocationStrategy } from "@angular/common";
import { AppComponent } from './app.component';
import {MyUIRouterConfig} from "./router.config";
import {
  UIROUTER_PROVIDERS, 
  UIView, 
  UIRouterConfig,
  UIROUTER_DIRECTIVES} from "ui-router-ng2";

if ('<%= ENV %>' === 'prod') { enableProdMode(); }

bootstrap(AppComponent, [
    ...UIROUTER_PROVIDERS,
    provide(UIRouterConfig, { useClass: MyUIRouterConfig }),
    provide(LocationStrategy, {useClass: HashLocationStrategy})
])
.catch(err => console.log(err));

Upvotes: 3

Related Questions