0

I've an angular project with the following routing structure, starting from the app root:

  // app-routing.module.ts
  const routes: Routes = [
    {
      path: 'admin',
      loadChildren: () => import('./features/admin/admin.module').then((m) => m.AdminModule)
    },
  ];
  // Import and export...

  // admin-rounting.module.ts
  const routes: Routes = [
    {
      path: 'admin',
      component: ShellComponent,
      children: [
        {
          path: 'editor',
          loadChildren: () => import('./editor/editor.module').then((m) => m.EditorModule),
        },
      ],
    },
  ];
  // Import and export...

  // editor-routing.module.ts
  const routes: Routes = [
    {
      path: '',
      component: EditorComponent,
    },
    {
      path: 'view-profile',
      component: ViewProfileComponent,
      outlet: 'profile',
    },
    {
      path: 'edit-profile',
      component: EditProfileComponent,
      outlet: 'profile',
    },
  ];
  // Import and export...

The root outlet it's in the AppComponent, afterwards the ShellComponent contains another router outlet that is passed as content to a SidebarComponent that displays it in a mat-sidenav-content, something like this:

    // app.component.html
    <router-outlet><router-outlet>

    // shell.component.html
    <sidebar>
        <router-outlet outlet></router-outlet>
    </sidebar>

    // sidebar.component.html
    <mat-sidenav-content>
        <div class="content">
            <ng-content select="[outlet]"></ng-content>
        </div>
    </mat-sidenav-content>

So when I navigate to /admin/editor everything works as expected: I can see the ShellComponent and the EditorComponent is shown in the right place. The problem starts inside the EditorComponent. This component contains a <profile></profile> where I want to display the <view-profile></view-profile> or <edit-profile></edit-profile> component using the outlet named profile (as you can see in the EditorRoutingModule configuration. Here the code:

   // editor.component.html
   <profile></profile>

   // profile.component.html
   <router-outlet name="profile"></router-outlet>

   // profile.component.ts
   ngOnInit(): void {
      this.router.navigate([{ outlets: { profile: ['view-profile'] } }], { relativeTo: this.route });
   }

The ProfileComponent loads but I don't see the <view-profile></view-profile> as I expect. Inspecting the HTML I can see the profile outlet where I expect it.

Any suggestion about it?

4

2 回答 2

0

不得不像这样将路径属性留空..

{ path: '', component: ViewProfileComponent, outlet: 'view-profile', pathMatch: 'full' }
于 2020-11-21T02:03:18.097 回答
0

我让它工作,但我不知道它是否应该是这样,或者它是否是一种解决方法。无论如何,我给了path根目录EditorRoutingModule并添加了命名路由作为它的子节点,如下所示:

  const routes: Routes = [
    {
      path: 'outlets',
      component: EditorComponent,
      children: [
        { path: 'view-profile', component: ViewProfileComponent, outlet: 'profile' },
        { path: 'edit-profile', component: EditProfileComponent, outlet: 'profile' },
      ],
    },
    { path: '', redirectTo: 'outlets', pathMatch: 'full' },
  ];

我不完全喜欢它,因为我的路线看起来像/admin/editor/outlets/(profile:edit-profile),如果没有路线中的部分,是否可以达到相同的结果/outlets/

于 2020-11-21T12:02:55.133 回答