Angular 2错误:无效的提供程序 - 仅允许Provider和Type的实例,得到:[object Object]

时间:2016-08-10 13:18:50

标签: angular angular-ui-router angular2-routing

我正在将ui-router2与angular2-rc-4集成,但我正在

  

错误:无效的提供程序 - 仅允许Provider和Type的实例,得到:[object Object]

以下是引导代码


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

以下是我的路由器配置。

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

3 个答案:

答案 0 :(得分:3)

如果输入以下内容,则会为一个简单的错字引发相同的错误:

{ provide: MyService, useValue: myServiceMock }

代替此:

provider

请注意provideprovide之间的区别。正确的是var id_control_access = $("#control_access_id").val();

希望这会有所帮助。

答案 1 :(得分:1)

听起来您正在使用Angular2版本,但尚不支持此类提供程序

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

AFAIR这是在RC.4或RC.3中引入的

请尝试改为

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

答案 2 :(得分:1)

问题得到解决。问题是如果你想在你现有的应用程序中集成ui-router-ng2,ui-router-ng2文档并不清楚。我有靴子AppComponent而不是UIView,这在文档中是不正确的。 以下是代码。

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