在這種情況下,我遇到了使用Angular 2的RC5和最新路由器的問題。Angular 2 RC5&@ angular/router 3.0.0-rc.1無效配置或錯誤?
我routes.ts文件是這樣的:
import {
provideRouter,
Routes,
RouterModule
}
from '@angular/router';
import {
OverviewComponent,
LoginComponent,
ProfileComponent
} from './shared';
import { AuthGuard } from './auth.guard';
import { HasToken } from './common/index';
const routes: Routes = [
{
path: '',
component: OverviewComponent,
canActivate: [AuthGuard]
},
{
path: 'login',
component: LoginComponent
},
{
path: 'profile',
component: ProfileComponent,
canActivate: [AuthGuard]
},
{
path: '**',
redirectTo: '/login'
}
];
export const authProviders = [AuthGuard, HasToken];
export const appRouterProviders = [
provideRouter(routes),
authProviders
];
export const routing = RouterModule.forRoot(routes);
而且我app.module.ts文件(引導)是這樣的:
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
import {BrowserModule} from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {appRouterProviders, routing} from './routes';
import {
OverviewComponent,
LoginComponent,
ProfileComponent
} from './shared';
@NgModule({
declarations: [
AppComponent,
OverviewComponent,
LoginComponent,
ProfileComponent
],
imports: [
BrowserModule,
CommonModule,
// Router
routing,
// Forms
FormsModule,
],
providers: [
appRouterProviders,
provide(AuthHttp, {
useFactory: (http) => {
return new AuthHttp(new AuthConfig({
headerName: 'Authorization',
headerPrefix: 'Bearer',
tokenName: 'token',
tokenGetter: (() => localStorage.getItem('token')),
globalHeaders: [{'Content-Type': 'application/json'}],
noJwtError: false,
noTokenScheme: false
}), http);
},
deps: [Http]
})
], entryComponents: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {
}
最後我項文件(main.ts)是這樣的:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode, provide } from '@angular/core';
import { Http } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { AppModule, environment } from './app/';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
所以,當我運行ng-serve
(這是一個角度,CLI可以的WebPack項目)即時得到這個電子RROR控制檯:
EXCEPTION: Error: Invalid configuration of route ' ': one of the following must be provided (component or redirectTo or children or loadChildren)
更新代碼和新的錯誤
Uncaught Unexpected value 'undefined' declared by the module 'AppModule'
最新的更新
似乎有與桶的問題。如果我進口部件到app.module
它繞過這個錯誤,但給人的另一種:
uri.match is not a function
我試過,當然添加pathMatch
屬性的途徑,但沒有改變。
似乎有一個問題的角度:https://github.com/angular/angular/pull/10595 –