【问题标题】:Can anyone tell me the flow of router that how it treats the route configuration and nested routes in lazy-loaded modules?谁能告诉我路由器的流程,它如何处理延迟加载模块中的路由配置和嵌套路由?
【发布时间】:2020-07-17 05:15:09
【问题描述】:

我希望我的子组件中的延迟加载模块参数化加载到一个 <router-outlet></router-outlet> 中。

我的 app.routing.module 是

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent },
  { path: 'reports', loadChildren: () => import('./reports/reports.module').then(m => m.ReportsModule) }
  { path: 'patients', loadChildren: () => import('./patients/patients.module').then(m => m.PatientsModule) },
  { path: 'consultant', loadChildren: () => import('./consultant/consultant.module').then(m => m.ConsultantModule) },
  { path: 'store', loadChildren: () => import('./store/store.module').then(m => m.StoreModule) },
  { path: 'attendence', loadChildren: () => import('./attendence/attendence.module').then(m => m.AttendenceModule) },
  { path: '' , redirectTo : 'dashboard', pathMatch: "full"},  
  { path: '**' ,component: PagenotfoundComponent }
];

app.component.html 是

<div id="app">
    <div>
        <div id="sidenav">
            <div id="logo"></div>
            <div routerLink="/dashboard" class="button" routerLinkActive="active">Dashboard</div>
            <div routerLink="/reports" class="button" routerLinkActive="active" >Reports</div>
            <div routerLink="/attendence" class="button"routerLinkActive="active">Attendence</div>
            <div routerLink="/patients" class="button"routerLinkActive="active">Patients</div>
            <div routerLink="/consultant" class="button"routerLinkActive="active" >Consultant</div>
            <div routerLink="/store" class="button"routerLinkActive="active">Store</div>
        </div>
    </div>
    <div id="main">
        <router-outlet></router-outlet>
    </div>
</div>

report.routing.module.ts 是

const routes: Routes = [
{ 
  path: '', component: ReportsComponent,
  children: [{ path : ':d_name' ,component: ReportdetailComponent }]
}];

report.module.ts 有一个共享模块,其组件&lt;app-datacard [depart]="depart"&gt;&lt;/app-datacard&gt; 被使用,并从父级和显示中获取数据。As

<p>reports works!</p>
<app-datacard [depart]="depart"></app-datacard>
<router-outlet></router-outlet>
<div class="card">
    <div  #card (click)="navigate(head.d_name)" class="card_child" *ngFor="let head of depart ;index as i">
      <div  *ngFor="let data of head | keyvalue : returnZero">
          <div *ngIf="data.key == 'd_name' ; then thenB; else elseB"></div>
          <ng-template #thenB>
            <h2>{{data.value}}</h2>
          </ng-template>
          <ng-template #elseB>
            <div class="data_detail">
              <div >{{data.key}}</div>
              <div>{{ data.value}}</div>
            </div>
          </ng-template>
      </div>
    </div>
  </div>

data-card.component.ts 有路由方法

navigate(card){
   this.router.navigate([card], {relativeTo: this.route})
}

哪个导航,但是当我导航到他们的孩子 pagenotfound 得到显示。 和 当我更改配置顺序时 report.routing.module.ts

const routes: Routes = [
{ path: '', component: ReportsComponent},
{ path : ':d_name' ,component: ReportdetailComponent }
];

它有效,但如何?

2 再次 当我在 app.routing.module.ts 中添加参数化路由时

如果将report.routing.module.ts的参数化路由添加到app.routing.module.ts 作为 报告模块.ts

const routes : Route = [{ path: '', component: ReportsComponent}];

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent },
  { path: 'reports', loadChildren: () => import('./reports/reports.module').then(m => m.ReportsModule) },
  { path : 'reports/:d_name' ,component : ReportdetailComponent },

  { path: 'patients', loadChildren: () => import('./patients/patients.module').then(m => m.PatientsModule) },

再次正常工作我没有得到获取路由配置的流路由器。 3 延迟加载的嵌套路由添加&lt;router-outlet&gt;&lt;/router-outlet&gt;是路由器的必要规则吗?

【问题讨论】:

    标签: angular routes lazy-loading router nested-routes


    【解决方案1】:

    您的设置适合​​路由。

    只需将this.route.navigate([card], {relativeTo: this.route}) 替换为

    this.route.navigate([card], {relativeTo: this.activatedRoute})
    

    所以在你的组件中注入ActivatedRoute并替换this.route

    对于游览问题为什么它起作用,这是因为您的 this.router 指向基本路线,当您使用没有子项的设置的参数导航到该路线时,您实际上指向了正确的路线。

    至于&lt;router-outlet&gt;&lt;/router-outlet&gt;,你需要它在父组件中让你的孩子渲染。

    但是如果你没有找到那个错误页面,那么它应该是navigate 方法问题

    【讨论】:

    • 是的,我也这样做了,但我问了 3 个问题?没有答案
    • report.routing.module.ts 现在是const routes: Routes = [ { path: '', component: ReportsComponent, children: [{ path : ':d_name' ,component: ReportdetailComponent }] }];,当我导航时。navigate(card){ this.router.navigate([card], {relativeTo: this.route}) }我得到了渲染的 PageNotfound 组件。
    • 这是因为你有relativeTo: this.route,我假设你不使用ActivatedRoute。你在为this.route注入什么
    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2019-09-29
    • 2017-08-21
    相关资源
    最近更新 更多