index.html页面加载(Angular 2 RC-7)但无法匹配任何路由:''在控制台中

时间:2016-09-26 17:27:55

标签: angular

首先,我正在构建我的前端并设置几个页面以相互路由,而不是我将为后端连接弹簧引导。

但是我的索引页面加载了,但是在我的控制台中出现以下错误。

core.umd.js:5995 EXCEPTION: Uncaught (in promise): Error: Cannot match any routes: ''

routes.config.ts

const appRoutes: Routes = [
    {path:'home', component: HomeComponent, useAsDefault: true},
    {path:'login', component: LoginComponent},
    //{ path: '**', component: Error404Page }
   // {path: '/register', name:'Register', component: Register},
];

export const appRoutingProviders: any = [

];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

当前只有登录页面准备就绪,这是组件:

@Component({
    moduleId: module.id,
    selector: 'login',
    templateUrl:'./login.html'
})

export class LoginComponent {

    login:LoginModel = new LoginModel();

    constructor(public loginService:LoginService){
        console.log("Inside constructor...")
    }

    onSubmit() {

    }
}

app.module.ts

@NgModule({
    imports: [BrowserModule, FormsModule, routing],

    declarations: [AppComponent,
        LoginComponent, HomeComponent,NavigationBar],

    providers:[LoginService, appRoutingProviders],

    bootstrap: [AppComponent]
})
export class AppModule {

}

我阅读了Angular的最新教程,看起来我正确地做到了。

1 个答案:

答案 0 :(得分:0)

您需要设置路由中不存在的默认路由

注意不确定但是删除了useAsDefault,但仍需要向某人确认

const appRoutes: Routes = [ 

{path:'',redirectTo:'home', pathMatch: 'full'}, //<<<===here 
{path:'home', component: HomeComponent}, 
{path:'login', component: LoginComponent}, 

];

还要确保 AppComponent 在HTML中的某处有<router-outlet>