简体   繁体   English

在 Angular 6 中使用延迟加载时,路由不适用于不同的路由

[英]routing is not working for different routes while using lazy loading in Angular 6

Actually i am facing problem while establishing lazy loading in my app.实际上我在我的应用程序中建立延迟加载时遇到了问题。 I am using angular 6. Here is scenario-我正在使用 angular 6. 这是场景-

In my app-routing.module.ts i have following routes-在我的 app-routing.module.ts 我有以下路线 -

const appRoutes: Routes = [

  {
    path: '',
    redirectTo: 'dashboard', pathMatch: 'full'
  },
  {
    path:'home',
    loadChildren: './main/home/home.module#HomeModule'
  }

  { path: '**', redirectTo: '/404' }
];

In my home.module.ts i have this routes-在我的 home.module.ts 中,我有这条路线-

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

  {
    path: '',
    component: HomeComponent
  },
  {
    path:'list',
    component: ListComponent
  }

];

Now want to navigate just like below-现在想像下面一样导航-

  1. /home---> HomeComponent /home---> 主页组件
  2. /list---> ListComponent /list---> 列表组件

I can navigate to HomeComponent but can't navigate to ListComponent.can anyone help me how can i achieve this.我可以导航到 HomeComponent 但无法导航到 ListComponent。任何人都可以帮助我如何实现这一目标。 Thank you谢谢

In order to access to list lazy loaded route the only way is through home route aka (localhost:4200/home/list)为了访问延迟加载路由列表,唯一的方法是通过主路由 aka (localhost:4200/home/list)

const appRoutes: Routes = [

  {
    path: '',
    redirectTo: 'dashboard', pathMatch: 'full'
  },
  {
    path:'home',
    loadChildren: './main/home/home.module#HomeModule'
  },

  { path: '**', redirectTo: '/404' }
];

const routes: Routes = [

  {
    path: '',
    component: HomeComponent
  },
  {
    path:'list',
    component: ListComponent
  }

];

According to code that you provided, all the components that come under the home module will start with route prefix home.根据您提供的代码,home 模块下的所有组件都将以路由前缀 home 开头。 After starting the application you can access sign-in component using:启动应用程序后,您可以使用以下方式访问登录组件:

http//:localhost:4200/home/list http//:localhost:4200/home/列表

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

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