Angular 具有多条路由和路由器出口的路由

Angular routing with multiple routes and router-outlet

除了应用程序根目录的导航之外,我还尝试向组件添加导航,但它没有导航到组件路由。

我正在使用两个路由器插座:

有两条路线:

当导航到“users”时,我可以看到 users.component.html 有两个 link,但是当按下 link“list”,它不会导航到:UsersListComponent 定义在 users.routing.module.ts

我不确定这样做是否正确。

下面是项目的文件夹结构和源代码:

-- app.routing.module.ts
-- app.component.html
-- components
   -- dashboard
   -- users
      -- users.component.html
      -- users.module.ts
      -- users.routing.module.ts

app.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UsersModule } from './panels/users/users.module';

const AppRouting: Routes = [
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: DashboardComponent },
    { path: 'users', loadChildren: () => UsersModule  }
    { path: '**', component: DashboardComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(AppRouting, { enableTracing: true })],
    exports: [RouterModule],
    providers: []
})
export class AppRoutingModule { }

app.component.html

<div class="app-wrapper">
    <router-outlet></router-outlet>
</div>

users.component.html

<a [routerLinkActive]="['active']" [routerLink]="[{ outlets: { 'users':['list'] } }]" [skipLocationChange]="true">List</a>&nbsp;
<a [routerLinkActive]="['active']" [routerLink]="['create']" [skipLocationChange]="true">Create</a>&nbsp;
<router-outlet name="users"></router-outlet>

users.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule, RouterOutlet, RouterLink, RouterLinkActive } from '@angular/router';
import { CreateUserComponent } from './user-create/user-create.component';
import { UsersListComponent } from './users-list/users-list.component';
import { UsersComponent } from './users.component';

const UsersRouting: Routes = [
     { path: 'list', component: UsersListComponent, outlet: 'users' },
     { path: 'create', component: CreateUserComponent, outlet: 'users' },
     { path: 'edit', component: CreateUserComponent, outlet: 'users' },
     { path: '', component: UsersComponent }
];

@NgModule({
  imports: [
      RouterModule.forChild(UsersRouting)
  ],
  declarations: [],
  exports: [
      RouterModule
  ],
})
export class UsersRoutingModule {}

users.module.ts

import { NgModule } from '@angular/core';
import { CreateUserComponent } from './user-create/user-create.component';
import { UsersComponent } from './users.component';
import { UsersListComponent } from './users-list/users-list.component';
import { UsersRoutingModule } from './users.routing.module';

@NgModule({
  imports: [
    UsersRoutingModule,
  ],
  declarations: [
    UsersComponent,
    UsersListComponent,
    CreateUserComponent
  ]
})
export class UsersModule { }

提前致谢

您真的需要命名路由器插座吗?

<router-outlet name="users"></router-outlet>

或者您可以只使用子路由器插座吗:

<router-outlet></router-outlet>

我在这里有一个关于路由的视频可能会有所帮助:https://www.youtube.com/watch?v=LaIAHOSKHCQ&t=1s

哟...只需将出口指定为自定义出口即可https://angular.io/guide/router#add-a-secondary-route

所以你可以说

{
  path: 'compose',
  component: ComposeMessageComponent,
  outlet: 'popup'
},

然后该组件将在 <popup></popup> 组件中呈现