我正在尝试使用类似于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