具有多个路由和路由器出口的角度路由

时间:2018-08-10 21:06:29

标签: angular routes angular-routing router-outlet

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

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

  • 应用程序根目录(app.component.html)的路由器出口
  • 用户组件的路由器插座(users.component.html)

有两条路线:

  • 应用程序的路由(app.routing.module.ts)
  • 用户路线(users.routing.module.ts)

导航到“ 用户”时,我可以看到带有两个链接的 users.component.html ,但是当按下链接“ 列表”时, ”,它不会导航到 users.routing.module.ts

中定义的 UsersListComponent

我不确定这是否是正确的方法。

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

-- 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 { }

预先感谢

2 个答案:

答案 0 :(得分:3)

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

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

或者您可以只使用子路由器出口吗?

<router-outlet></router-outlet>

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

答案 1 :(得分:0)

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

所以你可以说

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

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