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獲取路由如何工作的更多信息 –