我正在尝试在类似于此处发布的问题的子组件中使用辅助路由。由于发布的“解决方案”更像是一种解决方法,我很好奇如何像上面的博客文章中那样做。
我将 Angular 2.1.2 与路由器 3.1.2 一起使用。
父模块
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 { }
父组件
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>'
})
export class ParentComponent{ }
子模块
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 { }
子组件
import { Component } from '@angular/core';
@Component({
selector: 'child-view',
template: '<router-outlet name="child"></router-outlet>',
})
export class ChildComponent{}
因此,当我尝试浏览到 /(child:other) 时,我得到了典型的:
错误
Cannot find the outlet child to load 'OtherComponent'