简体   繁体   English

具有子路由的延迟加载模块中的角度路由

[英]angular routing in lazy loaded module with child routes

I have an app where every module is lazy loaded. 我有一个应用程序,其中每个模块都是延迟加载的。 The structure is: 结构是:

  • app 应用
    • grid-module 电网模块
    • dashboard-module 仪表板模块
      • dashboard-grid module 仪表板网格模块
      • chart-module 图表模块

I have spent all day trying to get links to dashboard-grid-module and chart-module to work but everything I try just makes it worse. 我花了一整天时间试图获得仪表板网格模块和图表模块的链接才能正常工作,但我尝试的所有内容都让它变得更糟。 It is now to the point where clicking the link for chart-module loads dashboard-module inside dashboard-module and causes call stack limit. 现在,点击图表模块的链接会加载仪表板模块内的仪表板模块并导致调用堆栈限制。 I have created an app on stackblitz with the setup. 我已经使用设置在stackblitz上创建了一个应用程序。 was hoping somebody could help me figure out what to do as the only thing I can think of is not to use routing at all. 我希望有人可以帮我弄清楚要做什么,因为我唯一能想到的就是根本不使用路由。 stack blitz app 堆栈闪电战应用程序

Edit for 1st answer: 编辑第一个答案:

ah, that is the issue with setting everything up manually. 啊,这是手动设置所有内容的问题。 sorry. 抱歉。 my actual app has those defined. 我的实际应用程序已定义。 the difference now between my stackblitz before and my app was that in my app I have to do router.navigate instead of routerlink. 我之前的stackblitz和我的应用程序之间的差异是在我的应用程序中我必须做router.navigate而不是routerlink。 so I have now updated it to have the definitions and use buttons with router.navigate. 所以我现在更新它以获得定义并使用router.navigate按钮。 I have also commented out everything I have tried with same results with changes 我还评论了我尝试过的所有相同结果的变化

Answer For Edit 1: 回答编辑1:

Change this: 改变这个:

<button (click)="doIt('lazy1')">Lazy 1</button> // wrong name
<button (click)="doIt('lazy2')">Lazy 2</button> // wrong name

this.router.navigate(['/' + route]);

To: 至:

<button (click)="doIt('lazychild1')">Lazy 1</button>
<button (click)="doIt('lazychild2')">Lazy 2</button>

this.router.navigate(['/lazy/' + route]);

https://stackblitz.com/edit/angular-rnehv2 https://stackblitz.com/edit/angular-rnehv2

Original Answer 原始答案

You are missing routing definitions in the child modules. 您缺少子模块中的路由定义。 Without the routing, the modules will not know which component to render. 如果没有路由,模块将不知道要呈现哪个组件。

Working Example: https://stackblitz.com/edit/angular-fkicz7 工作示例: https//stackblitz.com/edit/angular-fkicz7

const routes: Routes = [
    {
        path: '',
        component: LazyChild1Component
    }
];

export const routing = RouterModule.forChild(routes);

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

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