简体   繁体   English

带有延迟加载模块和子路由的Angular 7路由器'**'通配符不起作用?

[英]Angular 7 router '**' wildcard with lazy load module and child routes not working?

I'm trying to create a default route using the wildcard '**' from Angular's router. 我正在尝试使用来自Angular路由器的通配符'**'创建默认路由。 That default route will load a lazy module and then it will have to solve its own routes. 该默认路由将加载一个惰性模块,然后必须解决自己的路由。 The problem is that when I have the following configuration it does not resolve as expected: 问题是,当我进行以下配置时,无法按预期解决:

export const routes = [
  {
    path: '',
    component: 'DashboardComponent'
  },
  {
    path: '**',
    loadChildren: './lazy/lazy.module#LazyModule'
  }
];

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  declarations: [AppComponent]
  bootstrap: [AppComponent]
})
export class AppModule {}

const routes = [
  {
    path: '', 
    component: MainComponent
  }
  {
    path: 'hello',  // hoping to pick up the wildcard and resolve the route
    component: HelloComponent
  }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  declarations: [
    AnyComponent,
    EditComponent
  ]
})
export default class LazyModule {}

For example. 例如。 With mydomain.com/hello it does not show me the HelloComponent, it shows me the MainComponent. 使用mydomain.com/hello,它不会显示HelloComponent,而是显示MainComponent。

Is there something wrong with my configuration or should it not work like this? 我的配置有问题吗?还是不能这样工作?

Thanks in advance. 提前致谢。

The lazy modules require two routing configuration. 惰性模块需要两个路由配置。 The first one which is in the appModule tells the angular when to load the module. appModule中的第一个告诉角度何时加载模块。 The second one which is in the lazy feature module, tells the angular when to display a specific component. 第二个位于延迟特征模块中,告诉角度何时显示特定组件。

In your case, change the path of the appModule routing to be hello . 在您的情况下,请将appModule路由的路径更改为hello This tells the angular to download the lazy module when sees the hello url. 当看到hello URL时,这告诉Angular下载懒惰模块。 As of the second configuration, leave it empty. 从第二种配置开始,将其保留为空。 This tells angular to load the component when it sees an empty string following the hello url 当您在hello网址后看到一个空字符串时,这告诉angular加载组件

AppModule 应用模块

export const routes = [
  {
    path: '',
    component: 'DashboardComponent'
  },
  {
    path: 'helllo', <-- change this
    loadChildren: './lazy/lazy.module#LazyModule'
  }
];

LazyModule 懒模块

const routes = [
  {
    path: '', 
    component: LazyComponent // I do not know what this is. The components are not lazy. Modules are
  }
  {
    path: '',  <-- change this
    component: HelloComponent
  }
];

I saw in your code that you have a LazyComponent . 我在您的代码中看到您有一个LazyComponent I do not know what you are trying to achieve with this but the components are not lazy. 我不知道您想通过此实现什么,但是组件并不懒惰。 Modules are. 模块是。

I believe you have to redirect to an actual route. 我相信您必须重定向到实际路线。 There are a few topics related to this, here is one . 有与此相关的一些主题, 这里是一个 Also per Angular's examples , you might have to export your RouterModule from LazyModule. 同样根据Angular的示例 ,您可能必须从LazyModule导出RouterModule。

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

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