Angular Router:如何处理子模块路由器的委托

时间:2018-01-18 17:15:44

标签: angular angular-ui-router

您能否告诉我如何处理高级组件df1及其子组件def test(my_df=None): global df1 if not my_df: my_df = df1 print(my_df) df1 = ['a'] test() # returns ['a'] some_method() test() # returns dataframe 中的情况?

在我看来,在这种情况下,我需要将子组件放在自己的模块中,并使用router-outlet为其提供自己的路由器。配置完成后,我可以在router-outlet上显示主要组件,但在转到RouterModule.forChild时出现此错误:

localhost:4200/main

我在app.component.html:

localhost:4200/main/section

在此Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'main/section' 中,我想在<router-outlet></router-outlet>

下显示app.component.html

此MainViewComponent组件在其自己的模块中有自己的逻辑,可在其MainViewComponent中显示其子组件,并且可通过localhost:4200/main<router-outlet></router-outlet>等网址访问。

app / app-routing.module.ts(由app.module.ts导入)

localhost:4200/main/section

app / main-view-module / app-routing.module.ts(由app / main-view-module / main-view.module.ts导入)

localhost:4200/main/section2

2 个答案:

答案 0 :(得分:1)

您最好将所有路线放在一个地方,当1个分量$.ajax({ url: '/register.php', method: 'post', data: { 'url': 'http://...', 'user_id': user_id } });

存在于另一个组件中,你可以做到这一点

router-outlet

在其他情况下,组件只会改变另一个(谈论像你一样放置路线,但在一个地方)

实际上所有模块都导入到 const routes: Routes = [ { path: '', redirectTo: 'main', pathMatch: 'full' }, { path: 'main', component: MainViewComponent, children: [ { path: '', redirectTo: 'section', pathMatch: 'full' }, { path: 'section', component: SectionViewComponent }, { path: 'section2', component: Section2ViewComponent } ]} ]; ,模块更多用于代码抽象,因此您可以在一个地方构建路由层次结构,不会出错。此时,使用app.module选项就像说,我希望当我在子路线上时,此组件存在。

答案 1 :(得分:1)

经过进一步研究后:

我寻找的功能称为Lazy loading

为了让我的示例正常工作,我只需将loadChildren属性添加到path中的app/app-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'main', pathMatch: 'full' },
  { path: 'main', component: MainViewComponent,
    loadChildren: 'app/main-view-module/main-view.module#MainViewModule'
  }
];