【问题标题】:Angular default path for an outlet出口的角度默认路径
【发布时间】:2018-06-25 06:24:41
【问题描述】:

给定以下模块,我如何创建路由,以便当应用程序加载此模块时,它将路由到 CComponent 并在命名路由器出口 search-results 中加载 AComponent

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { AComponent } from './a.component';
import { BComponent } from './b.component';
import { CComponent } from './c.component';

@NgModule({
  declarations: [
    AComponent,
    BComponent,
    CComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot([
      {
        path: '',
        pathMatch: 'prefix',
        component: CComponent,
        children: [
          {
            path: 'a',
            component: AComponent,
            outlet: 'search-results'
          },
          {
            path: 'b',
            component: BComponent,
            outlet: 'search-results'
          }
       ]
    ])
  ],
  providers: [],
  bootstrap: [CComponent]
})
export class AppModule {}

a.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'a-component',
  template: `
      <div class="tabs row">
        <h3 [routerLink]="[{ outlets: { 'search-results': ['a'] } }]" class="tab" routerLinkActive="active">
          A
        </h3>
        <h3 [routerLink]="[{ outlets: { 'search-results': ['b'] } }]" class="tab" routerLinkActive="active">
          B
        </h3>
      </div>
    <router-outlet name="search-results"></router-outlet>
  `
})
export class AComponent {
}

我在路线上尝试了许多不同的东西:

通过上述配置,页面加载,但AComponent 没有加载到屏幕上。

如果我将CComponent 更新为具有以下内容:

export class CComponent {
  constructor(
    private route: ActivatedRoute,
    private router: Router
  ) {
    router.navigate(
      [
        {
          outlets: {
            'search-results': ['a']
          }
        }
      ], { relativeTo: route });
  }
}

然后似乎一切正常,但必须在父元素的构造函数中触发导航似乎是错误的。

如果我更新子路由并将{ path: 'a', component: AComponent, outlet: 'search-results' } 替换为{ path: '', component: AComponent, outlet: 'search-results' },路由器似乎会在插座中正确加载该组件,但routerLinkActive 指令似乎不像active 类那样生效未添加到第一个 h3 并且将 routerLink 更新为 outlets: { 'search-results': ['a'] } 不允许导航到 BComponent 后导航回 AComponent

我在上面尝试了许多变体,但都没有成功。

有没有办法配置路由,以便默认路由将在未命名的router-outlet 和命名search-results 路由器出口中的AComponent 中加载CComponent

Plunkr

【问题讨论】:

    标签: angular angular-routing angular-router angular-router-loader


    【解决方案1】:

    当前,您正在定义指向基本路由的子路由,其中​​之一是/a(将AComponent 加载到search-results 出口的路径),但是当应用程序加载。当您强制 navigate 到您的 CComponent 中的该路由时,它会起作用(在加载时会被初始化)。

    如果您希望应用程序在 (search-results):a 路由处于活动状态的情况下初始加载,您可以在路由定义中使用 redirectTo property

    在你的情况下,我会在初始路由上进行模式匹配(空路径:'')并重定向到加载了/a 的路径,如下所示:

    RouterModule.forRoot([
          /* this line will match the initially loaded route and 
           immediately redirect to the child route 'a', which loads 
           `AComponent` into the `search-results` outlet */
          { path: '', pathMatch: 'full', redirectTo: '(search-results:a)'},
    
          {
            path: '',
            pathMatch: 'prefix',
            component: CComponent,
            children: [
              {
                path: 'a',
                component: AComponent,
                outlet: 'search-results'
              },
              {
                path: 'b',
                component: BComponent,
                outlet: 'search-results'
              }
           ]
          }
        ])
    

    请注意,上面的 sn-p 中的顺序很重要。它将匹配空路径,重定向到子路由,并在指定的插座中加载您想要的组件(如您在代码中定义的那样)。

    【讨论】:

    • 谢谢,但这给了我控制台中的错误 Error: Cannot match any routes. URL Segment: 'a' 并且没有加载。
    • 当您在CComponent 构造函数中使用navigate 方法加载应用程序时,URL 中显示的路由是什么?
    • http://localhost:4200/c/(search-results:a)。我试过重定向到(search-results:a) 和一些类似的组合,但没有运气。
    • 问题只是重定向中的“/a”。根据您问题中的代码,我无法获得您需要的确切路线的上下文,但您只需将上面“redirectTo”属性中的值更改为AComponent 加载的路线(也就是获得的路线)在 CComponent 中使用 navigate 时加载。尝试重定向到 '/c/(search-results:a)'
    • 我有,但它不起作用。我很高兴制作 jsFiddle 或类似的东西,但不知道在哪里/如何制作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多