i使用Angular7进行Web应用程序项目。我遇到了一个问题,路由器无法用this.router.navigate([/home/home/fetch-data)导航到子组件。)。
import { FetchDataComponent } from './fetch-data/fetch-data.component';
import { CounterComponent } from './counter/counter.component';
import { HomeComponent } from './components/home/home.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
data: { bc: "NAVI.HOME" },
children: [
{
path: 'fetch-data',
component: FetchDataComponent,
data: { bc: "VEHICLE.DETAILS" }
}
]
},
{
path: 'counter',
component: CounterComponent,
data: { bc: "Counter" }
},
// {
// path: 'home/fetch-data',
// component: FetchDataComponent,
// data: { bc: "Home > Counter" }
// },
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
}
];
@NgModule({
declarations: [],
imports: [
RouterModule.forRoot(routes),
CommonModule,
],
exports: [RouterModule]
})
export class AppRoutingModule { }
,搜索组件将触发一个函数:
public naviTo(): void {
this.vehicleService.getVehicleByVin(this.vehicle.vin);
this.router.navigate(['../../home/fetch-data']);
this.messageService.sendMessage("VehicleData");
}
和app.component.html
<mat-sidenav-container class="mat-sidenav-container" autosize>
<mat-sidenav mode="side" class="mat-sidenav" opened>
<app-side-nav></app-side-nav>
</mat-sidenav>
<mat-sidenav-content>
<div class="app-header">
<app-header></app-header>
</div>
<div class="mat-sidenav-content">
<main class="content">
<app-breadcrumb></app-breadcrumb>
<router-outlet></router-outlet>
</main>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
都是相关的代码。函数navito()在search.component中,此搜索组件是标头组件的子组件。home.com.ponent在Navi的侧面导航。
形成了旋转器的定义,人可以理解,fetch-data组件是家庭的子部分,但它不是实现的真实亲子关系。
问题在于,它不会将其与函数navito()构成的孩子相处。我尝试了许多可能性,但它没有起作用。也许我已经过去了。
有什么解决方案?
最好的问候
leo
在这里您只需要相对于家庭组件(parent)的./
。
解决方案:
public naviTo(): void {
this.vehicleService.getVehicleByVin(this.vehicle.vin);
this.router.navigate(['./fetch-data']);
this.messageService.sendMessage("VehicleData");
}
希望这个帮助!