1
我有一个酒吧组件,然后我在酒吧组件下创建一个子组件(子栏)。我也有孩子的路线。但是当我使用子路由时,我的子组件找不到(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' },
];
请让我知道如果你需要更多的文件,我可以将它。谢谢。
如果我添加了这一行:{path:'',redirectTo:'child-bar',pathMatch:'full'} 子栏组件将显示出来,而不点击子栏组件链接,这不是我想。我想我们点击子栏链接后,子栏组件显示会更好 – EntryLeveDeveloper
如果我添加以下这行:{path:'child-bar',component:ChildBarComponent} 子栏组件将在我点击后显示子栏链接,但它没有指引我到另一个视图,但子栏组件显示在条组件的底部,这是不正确的 – EntryLeveDeveloper
@CodeContributor在正常情况下我们会自动打开一个子组件。如果你不想加载它,请移除redirectTo。如果你想重定向到新的视图,那么你为什么要创建它作为子组件。使它成为一个独立的组件老板。检查你的HTML设计在你想要的地方,并通过https://angular.io/guide/router获取路由如何工作的更多信息 –