Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
  }
}
like image 442
Shamsher Avatar asked Aug 10 '16 13:08

Shamsher


3 Answers

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.

like image 115
György Balássy Avatar answered Oct 19 '22 03:10

György Balássy


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));
like image 21
Shamsher Avatar answered Oct 19 '22 05:10

Shamsher


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 {}
like image 2
Günter Zöchbauer Avatar answered Oct 19 '22 03:10

Günter Zöchbauer