我正在嘗試使用類似於here的問題的子組件中的Auxiliary Routes。由於發佈的「解決方案」更像是一種解決方法,我很好奇如何在上面的博客帖子中做到這一點。在嵌套子組件中使用輔助路由
我在路由器3.1.2上使用Angular 2.1.2。
parent.module
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { ParentComponent } from './parent.component';
import { ChildModule } from '../child/child.public.module';
import { ChildComponent } from '../child/child.public.component';
import { OtherModule } from '../other/other.public.module'
@NgModule({
imports: [
ChildModule,
OtherModule,
RouterModule.forRoot([
{ path: '', component: ChildComponent }
])
],
declarations: [ ParentComponent ],
bootstrap: [ ParentComponent ]
})
export class ParentModule { }
parent.component
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>'
})
export class ParentComponent{ }
child.module
import { NgModule } from '@angular/core';
import { RouterModule} from '@angular/router';
import { CommonModule } from '@angular/common';
import { ChildComponent } from './child.public.component';
import { OtherComponent } from '../other/other.public.component'
@NgModule({
imports: [
CommonModule,
OtherModule,
RouterModule.forChild([
{path: 'other', component: OtherComponent, outlet: 'child'}
])
],
declarations: [ ChildComponent ],
exports: [ ChildComponent ],
})
export class ChildModule { }
child.component
import { Component } from '@angular/core';
@Component({
selector: 'child-view',
template: '<router-outlet name="child"></router-outlet>',
})
export class ChildComponent{}
所以,當我嘗試瀏覽/(子:其他),我得到的典型:
錯誤
Cannot find the outlet child to load 'OtherComponent'
至於我可以看到它,這是同樣的解決方案,我在上面提到的這個線程:http://stackoverflow.com/questions/39893428/auxiliary-router-outlet-inside-primary-router-outlet 事實上,路線是加載由父模塊通過路由器實例(.forChild)。否則,它會爲丟失的路由拋出錯誤。 主要的問題是,路由器不知道在哪裏可以找到嵌套的路由器插座。 – Usche