简体   繁体   English

prview组件内部模块延迟加载angular问题

[英]prview component inside module lazy loading angular problem

hi everybody I have built an angular app with lazy loading and everything works well except when I try to open child or pages inside modules it redirects me in anew page not inside the main one my structure is admin main module with component then user component and setting component my admin routing is大家好,我已经构建了一个具有延迟加载的 angular 应用程序,并且一切正常,除非我尝试在模块内打开子页面或页面时,它会将我重定向到新页面而不是主页面我的结构是管理主模块,其中包含组件然后是用户组件和设置组件我的管理路由是

const routes: Routes = [
  { path : '' , component: AdminComponent},
  { path : 'User' , component: UserComponent},
  { path : 'Settings' , component: SettingsComponent},
  { path: '**', component: SystemErrorComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }

I have added links and router-outlet inside admin HTML page我在管理员 HTML 页面中添加了链接和路由器插座

<div class="container">
  <button routerLink="" class="btn btn-sm btn-link">admin</button>
  <button routerLink="Settings" class="btn btn-sm btn-link">Settings</button>
  <button routerLink="User" class="btn btn-sm btn-link">User</button>
  <hr />


  <router-outlet></router-outlet>
  </div>

my app routing is我的应用路由是

const routes: Routes = [常量路线:路线= [

  { path: '', component: IndexComponent },
  { path: 'home', loadChildren: './home/home.module#HomeModule'},
  { path: 'admin', loadChildren: './admin/admin.module#AdminModule', canActivate: [AuthGuard]},
  { path: '**', component: PageNotFoundComponent },

];


@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }

when I open admin area it opened but when I click user it should open inside admin but it opened outside admin in a new page当我打开管理区域时它打开但是当我单击用户时它应该在管理员内部打开但它在新页面中在管理员外部打开

why that happened as from my thought there is no error happened为什么会发生这种情况,因为我认为没有发生错误

to be able to open "user" and "settings" components inside admin components the routing structure should be:为了能够在管理组件中打开“用户”和“设置”组件,路由结构应该是:

- Admin
--- User
--- Settings

Only this way it is possible.只有这样才有可能。 So change your routings of AdminRoutingModule to:因此,将AdminRoutingModule的路由更改为:

const routes: Routes = [
  {
    path: '',
    component: AdminComponent,
    children: [
      { path: 'User', component: UserComponent },
      { path: 'Settings', component: SettingsComponent },
      { path: '', redirectTo: 'User', pathMatch: 'full' },
    ],
  },
  { path: '**', component: SystemErrorComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdminRoutingModule { }

Angular shows only the child route's components in place of <router-outlet> . Angular 仅显示子路由的组件来代替<router-outlet> So let him know the correct routes structure.所以让他知道正确的路线结构。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM