我有一个Bar组件,然后在Bar Component下创建一个子组件(child-bar)。我也有儿童路线。但是当我使用子路径时,我的子组件找不到(404)。
app.routes.ts:
import { Routes } from '@angular/router';
import { HomeComponent } from './home';
import { AboutComponent } from './about';
import { BarComponent } from './bar/bar.component';
import { NoContentComponent } from './no-content';
import { DataResolver } from './app.resolver';
export const ROUTES: Routes = [
{ path: '', component: HomeComponent },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'bar', component: BarComponent},
{ path: 'detail', loadChildren: './+detail#DetailModule'},
{ path: 'barrel', loadChildren: './+barrel#BarrelModule'},
{ path: '**', component: NoContentComponent },
];
bar.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'bar',
templateUrl: './bar.component.html',
styleUrls: ['./bar.component.css']
})
export class BarComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
bar.component.html:
<p>
bar works!
</p>
<span>
<a [routerLink]=" ['./child-bar'] ">
Child Bar
</a>
</span>
<router-outlet></router-outlet>
子-bar.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'child-bar',
templateUrl: './child-bar.component.html',
styleUrls: ['./child-bar.component.css']
})
export class ChildBarComponent implements OnInit {
constructor() { }
ngOnInit() {
console.log('hello `ChildBar` component');
}
}
子-bar.module.ts:
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { routes } from './child-bar.routes';
import { ChildBarComponent } from './child-bar.component';
console.log('`ChildBar` bundle loaded asynchronously');
@NgModule({
declarations: [
/**
* Components / Directives/ Pipes
*/
ChildBarComponent,
],
imports: [
CommonModule,
FormsModule,
RouterModule.forChild(routes),
],
})
export class ChildBarModule {
public static routes = routes;
}
子-bar.routes.ts:
import { ChildBarComponent } from './child-bar.component';
export const routes = [
{ path: '', component: ChildBarComponent, pathMatch: 'full' },
];
如果您需要更多文件,请告诉我,我可以附上。感谢。
答案 0 :(得分:1)
如果某个组件是子组件,那么您的路由文件是错误的,那么路由文件应该是这样的
如果是没有延迟加载的正常路由
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'bar', component: BarComponent,
children: [
{ path: '', redirectTo: 'child-bar', pathMatch: 'full' },
{ path: 'child-bar', component: ChildBarComponent }
]
}
];
如果您想延迟加载,可以尝试这两种方式
1)
{ path: 'bar', component: BarComponent,
children: [
{ path: '', redirectTo: 'child-bar', pathMatch: 'full' },
{ path: 'child-bar', loadChildren: 'childbar/childbar.module#ChildBarModule' }
]
}
2)将带有子组件的完整条组件移动到具有自己的路由表的单独模块中
export const routes: Routes = [
{ path: '', redirectTo: 'product-list', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'bar', loadChildren: 'bar/bar.module#LazyModule' }
}
];
检查这个plnkr以了解延迟加载是如何工作的https://plnkr.co/edit/vpCqRHDAj7V6mlN1AknN?p=preview我希望这会有所帮助:)