简体   繁体   中英

child Routing in angular2 getting error

My Page Layout look like this one

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

if user click on Page1 i want to Refresh the Menus on the Left to list children for Page1, if i click Page 2 than i want to refresh the list of menu and display childrens of Pages 2

This is how my router look currently, but i am getting the error

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

Error: Cannot match any routes. URL Segment: '' at 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 { }

screenshot the UI looks as expected but there is error in the console window

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 

在此输入图像描述

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'
    ];

}

Now as soon as i update the Pag1Component to use named outlet it throws parsing error.

{{item}}

This just blows out the whole page and nothing is displayed, console window throws the parsing error

Error: (SystemJS) Template parse errors:(…)(anonymous function) @ (index):20ZoneDelegate.invoke @ zone.js:232Zone.run @ zone.js:114(anonymous function) @ zone.js:502ZoneDelegate.invokeTask @ zone.js:265Zone.runTask @ zone.js:154drainMicroTaskQueue @ zone.js:401ZoneTask.invoke @ zone.js:339

Is there a better way of doing this

Because there's no root path in the children. When the Page1 path first loads, it has a router outlet where it tries to load child routes, but with Page1 there is a route configured with '' . This would be required when you go to the Page1 url. If you went to Page1/Page1Child1 , it knows the component, but Page1 (which would need to map to '' ) doesn't exist

So just add a redirect to the default child route

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM