简体   繁体   English

使用模块和延迟加载在angular 7中嵌套路由

[英]Nested routing in angular 7, using modules and lazy loading

I am having trouble creating angular routes. 我在创建角度路线时遇到麻烦。 I have implemented feature modules and I am using routing for each of those modules. 我已经实现了功能模块,并且正在为每个这些模块使用路由。 I want the routes to go from app to layout to dashboard and then to accounts. 我希望路线从应用程序到布局再到仪表板,再到客户。 The route is not created properly, it seems dashboard is being skipped and route goes directly to accounts. 路由创建不正确,似乎仪表板已被跳过,路由直接进入了帐户。 Here is what it looks like when i use augury to debug the routes. 这是我使用augory调试路由时的外观。

在此处输入图片说明

Here is my code for each route. 这是我的每条路线的代码。 each route is in a module of its own. 每条路线都在自己的模块中。 I would really like to keep the routes separated in modules. 我真的很想将路由分隔在模块中。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

 //app routes const appRoutes: Routes = [ { path: "login", component: LoginComponent }, { path: "", canActivate: [AuthGaurdService], loadChildren: "./layouts/layouts.module#LayoutsModule" }, { path: "**", component: PageNotFoundComponent } ]; //layout routes const LayoutRoutes: Routes = [ { path: "", component: LayoutsComponent, canActivate: [AuthGaurdService], children: [ { path: "", canActivateChild: [AuthGaurdService], children: [ { path: "account-information", loadChildren: "../account-information/account-information.module#AccountInformationModule" }, { path: "", loadChildren: "../dashboard/dashboard.module#DashboardModule" }, ] } ] } ]; //dashboard routes const routes: Routes = [ { path: "", component: DashboardComponent, canActivate: [AuthGaurdService], children: [{ path: "", canActivateChild: [AuthGaurdService], children: [ { path: "accounts", loadChildren: "./accounts/accounts.module#AccountsModule" }, { path: "", component: HomeComponent } ] }] } ]; //account routes const AccountsRoutes: Routes = [ { path: "accounts", component: AccountsComponent, canActivate: [AuthGaurdService], children: [ { path: "", canActivateChild: [AuthGaurdService], children: [ { path: "transactions", component: TransactionsComponent }, { path: "summary", component: SummaryComponent }, { path: "earnings", component: EarningsComponent } ] } ] } ]; 
 <!--app.html --> <router-outlet></router-outlet> <!-- layout.html --> <div class="page-container"> <app-navbar></app-navbar> <div class="page-content-wrapper "> <div class="content"> <router-outlet></router-outlet> </div> <div class="container-fluid container-fixed-lg footer"> <app-footer></app-footer> </div> </div> </div> <!-- dashboard.html --> <div class="page-container"> <app-navbar></app-navbar> <div class="page-content-wrapper "> <div class="content"> <router-outlet></router-outlet> </div> <div class="container-fluid container-fixed-lg footer"> <app-footer></app-footer> </div> </div> </div> <!-- accounts --> <router-outlet></router-outlet> 

this is what my page looks like when i to home 这是我回家时页面的外观 在此处输入图片说明

this is the for accounts/summary 这是帐户/摘要的 在此处输入图片说明

and my side menu looks like this let retail: Array<Menu> = [ { name: "Home", router_link: "", submenu: [], toggle_submenu: false, icon: "" }, { name: "Accounts", router_link: "accounts/summary", toggle_submenu: false, icon: "", submenu: [ { name: "Summary", router_link: "accounts/summary", toggle_submenu: false, icon: "", submenu: [] }, { name: "Tansactions", router_link: "accounts/transactions", toggle_submenu: false, icon: "", submenu: [] }, { name: "Earnings", router_link: "accounts/earnings", toggle_submenu: false, icon: "", submenu: [] } ] }, { name: "Sales", router_link: "sales", toggle_submenu: false, icon: "", submenu: [] }, { name: "Inventory", router_link: "inventory", toggle_submenu: false, icon: "", submenu: [] }, { name: "Upload", router_link: "upload", toggle_submenu: false, icon: "", submenu: [] } ]; 我的侧边菜单如下所示let retail: Array<Menu> = [ { name: "Home", router_link: "", submenu: [], toggle_submenu: false, icon: "" }, { name: "Accounts", router_link: "accounts/summary", toggle_submenu: false, icon: "", submenu: [ { name: "Summary", router_link: "accounts/summary", toggle_submenu: false, icon: "", submenu: [] }, { name: "Tansactions", router_link: "accounts/transactions", toggle_submenu: false, icon: "", submenu: [] }, { name: "Earnings", router_link: "accounts/earnings", toggle_submenu: false, icon: "", submenu: [] } ] }, { name: "Sales", router_link: "sales", toggle_submenu: false, icon: "", submenu: [] }, { name: "Inventory", router_link: "inventory", toggle_submenu: false, icon: "", submenu: [] }, { name: "Upload", router_link: "upload", toggle_submenu: false, icon: "", submenu: [] } ];

As I havn't seen your complete code, my guess is that your not able to properly navigate to your routes. 由于我没有看到完整的代码,因此我猜测您无法正确导航到您的路线。

When you use lazy loaded modules, you don't need to provide the the prefix in child routes. 使用延迟加载的模块时,无需在子路由中提供前缀。 For example In your AppModule : 例如在您的AppModule

{
    path: '', component: HomeComponent
    },
   {path: 'dashboard',
    loadChildren: './dashboard/dashboard.module#DashboardModule' 
  }

And in your DashboardModule 并在您的DashboardModule

{
    path: '', component: DashboardComponent,
  },{
    path: 'accounts',
    loadChildren: '../accounts/accounts.module#AccountsModule' 
  }

Then in order to go to a route in your dashboard.component.html You will write 然后,为了转到您的dashboard.component.html中的路由,您将编写

<a [routerLink]="['accounts']"> Account</a>

Instead of 代替

<a [routerLink]="['/dashboard', 'accounts']"> Account</a>

because in child lazy loaded modules the root path starts from the parent module which loaded it. 因为在子级惰性加载的模块中,根路径从加载它的父级模块开始。

I have created a stackblitz demo with a similar scenario. 我创建了一个具有类似情况的stackblitz 演示

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

Hello friends i figured out the issue. 您好朋友,我知道了这个问题。 I removed the import of accounts module in dashboard module. 我在仪表板模块中删除了帐户导入模块。 This made sure that when routes where generated accounts route was placed after dashboard. 这样可以确保将生成帐户路由的路由放置在仪表板之后。 This is because I am lazy loading the modules. 这是因为我懒于加载模块。 This is what my routes looked like after This is what my routes looked like after 这是我的路线后的样子这是我的路线后的样子

Also make sure to add / in the child routes, so angular appends this route to its parent, ie accounts summary routerlink looks like this in my sidebar '/accounts/summary'. 还请确保在子路由中添加/,以便将此路由附加到其父路由,即帐户摘要routerlink在我的侧边栏“ / accounts / summary”中看起来像这样。

Every thing else stayed the same. 其他所有东西都保持不变。 Now my accounts summary component was placed inside of my dashboard router link and i displayed next to my sidebar 现在,我的帐户摘要组件被放置在仪表板路由器链接的内部,并显示在侧边栏旁边

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

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