子路由在angular2获取错误

时间:2016-10-23 03:24:30

标签: angular angular2-router3

我的页面布局看起来像这个

-----------------------------------------------------
HEADER MENU --> Page1  <Page2>
-----------------------------------------------------
Page1 Child1 |
Page1 Child2 |       MAIN CONTENT
Page1 Child2 |
----------------------------------------------------
          FOOTER
----------------------------------------------------

如果用户点击Page1我想刷新左边的菜单列出Page1的子项,如果我点击第2页比我要刷新菜单列表并显示第2页的子项

这是我的路由器当前的样子,但我收到了错误

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

错误:无法匹配任何路线。网址细分:''     在ApplyRedirects.noMatchError

的index.html

index.html

<div class="container">
    <div class="row">
    
    </div>
    <div class="row">
        <div class="col-md-2"><router-outlet></router-outlet></div>
        <div class="col-md-10"><router-outlet name="main"><h2>Main Content Here</h2></router-outlet></div>
    </div>
</div>

app.module.ts


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { Page1Component } from './Page1/Page1.component';
import { Page1Child1Component } from './Page1/Page1Child1/Page1Child1.component';
import { Page2Component } from './Page2/Page2.component';
import { RouterModule } from '@angular/router';


@NgModule({
    imports: [BrowserModule,

            RouterModule.forRoot([
                { path: '', redirectTo: 'Page1', pathMatch: 'full' },
                {
                    path: 'Page1', component: Page1Component,
                    children: [
                        { path: '', redirectTo: 'Page1Child1', pathMatch: 'full' },
                        { path: 'Page1Child1', component: Page1Child1Component }
                    ]

                },
                { path: 'Page2', component: Page2Component }
            ])
        ],
    declarations: [AppComponent, Page1Component, Page2Component, Page1Child1Component],
    bootstrap: [AppComponent]
})
export class AppModule { }

截图UI看起来如预期,但控制台窗口中出现错误

core.umd.js:3070 EXCEPTION: Uncaught (in promise): Error: Cannot find primary outlet to load 'Page1Child1Component'
Error: Cannot find primary outlet to load 'Page1Child1Component'
    at getOutlet 

enter image description here

Page1.component

import { Component } from '@angular/core';
@Component({
    selector: 'Page1',
    template: `<h1>This is from Page1 </h1>
    <ul>
  <li *ngFor="let item of links; let i = index">
        <a [routerLink]="['Page1Child1']" routerLinkActive="active" outlet:"main">{{item}}</a>
  </li>
</ul>
   `
})
export class Page1Component {
    links: any[] = [
    'Page1 Child1',
    'Page1 Child2'
    ];

}

现在,只要我更新Pag1Component以使用命名出口,它就会抛出解析错误。

{{项}}

这只是吹出整个页面而没有显示任何内容,控制台窗口会抛出解析错误

错误:(SystemJS)模板解析错误:(...)(匿名函数)@(索引):20ZoneDelegate.invoke @ zone.js:232Zone.run @ zone.js:114(匿名函数)@ zone.js: 502ZoneDelegate.invokeTask @ zone.js:265Zone.runTask @ zone.js:154drainMicroTaskQueue @ zone.js:401ZoneTask.invoke @ zone.js:339

有更好的方法吗

1 个答案:

答案 0 :(得分:0)

因为孩子们没有根路径。当Page1路径首次加载时,它有一个路由器出口,它尝试加载子路由,但Page1有一个配置了''的路由。当你转到Page1网址时,这是必需的。如果您转到Page1/Page1Child1,它会知道该组件,但Page1(需要映射到'')并不存在

所以只需将重定向添加到默认子路径

children: [
  { path: '', redirectTo: 'Page1Child', pathMatch: 'full' }
  { path: 'Page1Child1', component: Page1Child1Component}
]