简体   繁体   English

带有延迟加载的 Angular routerLink

[英]Angular routerLink with lazy loading

I apologize if this question has been answered.如果此问题已得到解答,我深表歉意。 I haven't found a problem like mine.我还没有发现像我这样的问题。

I have an angular app that has two 'pages'.我有一个有两个“页面”的角度应用程序。 Pages = normal components with modules.页面 = 带有模块的普通组件。 And I have a bunch of 'components' that are connected in one module.我有一堆连接在一个模块中的“组件”。 ( Here is the file structure - https://i.imgur.com/fs6tXgg.png ) (这是文件结构 - https://i.imgur.com/fs6tXgg.png

Now, in the app-routing.module , I use lazy loading to load the two pages.现在,在app-routing.module ,我使用延迟加载来加载两个页面。 Like this:像这样:

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./pages/user/user.module').then(mod => mod.UserModule)
  },
  {
    path: '',
    loadChildren: () => import('./pages/admin/admin.module').then(mod => mod.AdminModule)
  }
];

This works well.这很好用。 In the user page I load the main-menu component.在用户页面中,我加载了主菜单组件。 ..<app-main-menu></aapp-main-menu>...

PROBLEM - But now, I want to add routerLink="/hotels to a button in the main-menu component, in the user-page, so when the user clicks the button the page will load a different component, item-list. (not main-menu)问题- 但现在,我想将routerLink="/hotels添加到主菜单组件中的按钮,在用户页面中,因此当用户单击按钮时,页面将加载不同的组件,项目列表。(不是主菜单)

I tried adding a new path in the app-routing.module.ts :我尝试在app-routing.module.ts添加一个新路径:

  {
    path: 'item-list',
    component: ItemListComponent
  }

But I get an error saying that但我得到一个错误说

Component MainMenuComponent is not part of any NgModule or the module has not been imported into your module. Component MainMenuComponent 不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中。

The reason for this error is because the item-list.component is loaded into the user-components.module.ts, which is called in the user.module.ts (main module that the page loads).出现这个错误的原因是item-list.component加载到user-components.module.ts中,在user.module.ts(页面加载的主模块)中调用。

So the global question is: How can I set a routerLink to a component when using Lazy Loading (Because the component I want is declared in a specific module).所以全局问题是:如何在使用延迟加载时将 routerLink 设置为组件(因为我想要的组件是在特定模块中声明的)。

Kinda fixed the problem by calling the module that has the component in the app-routing.module.ts Kinda 通过调用 app-routing.module.ts 中包含该组件的模块来解决该问题

  {
    path: 'list',
    loadChildren: () => import('./components/user-components/user-components.module').then(mod => mod.UserComponentsModule)
  }

The user.module has: user.module 有:

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

...
  imports: [
    RouterModule.forChild(routes)
  ]

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

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