Angular 2错误:无效的提供程序 – 仅允许Provider和Type的实例,
发布时间:2020-12-17 10:23:29 所属栏目:安全 来源:网络整理
导读:我正在将ui-router2与angular2-rc-4集成,但我得到了 Error: Invalid provider – only instances of Provider and Type are allowed,got: [object Object] 以下是引导代码 import {trace,UIROUTER_PROVIDERS,UIView,UIRouterConfig,Category,UIROUTER_DIRECT
我正在将ui-router2与angular2-rc-4集成,但我得到了
以下是引导代码 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 }),{ 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); } }
问题得到了解决.问题是如果你想在你现有的应用程序中集成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,UIROUTER_DIRECTIVES} from "ui-router-ng2"; if ('<%= ENV %>' === 'prod') { enableProdMode(); } bootstrap(AppComponent,[ ...UIROUTER_PROVIDERS,{useClass: HashLocationStrategy}) ]) .catch(err => console.log(err)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |