2

我正在创建一个包含多个页面的 Web 应用程序,每个页面中都有动态部分。我希望这些部分中的每一个都使用角度路由器。

我试过简单地在组件中放置一个命名的路由器插座,但它似乎不起作用......有什么想法吗?

这是我的一般设置。

应用模板

<main>
  <router-outlet></router-outlet>
</main>

应用模块

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

@NgModule({
  imports: [PageModule, RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

页面模板:

<tabset>
  <tab>
    <router-outlet name="section1"></router-outlet>
  </tab>
  <tab>
    <router-outlet name="section2"></router-outlet>
  </tab>
</tabset>

页面模块

const routes: Routes = [
  {
    path: '',
    component: PageComponent,
  },
  {
    path: 'section1-view1',
    component: View1Component,
    outlet: 'section1',
  },
  {
    path: 'section1-view2',
    component: View2Component,
    outlet: 'section1',
  }
  // More routes ...
];

@NgModule({
  declarations: [
    PageComponent,
    View1Component,
    View2Component
  ],
  imports: [
    RouterModule.forChild(routes),
    CommonModule,
    TabsetModule
  ]
})
export class PageModule { constructor(){} }
4

2 回答 2

1

在新模块中创建每个组件,然后在该模块中定义内部路由,因此无论何时路由到模块,都可以在内部路由中路由。

保持当前路由不变,并声明新模块(将这些模块导入app.module),并在该模块中创建要在该模块中路由的新组件。

看看这个:Stackblitz只是一个供您使用的示例。

这里有一个组件app.module和一个模块intmodule,其中有两个组件intmodule。从app.module我们可以在hello.component和之间路由intmoduleintmodule我们可以在comp1和comp2之间路由。

评论以获得更多帮助:)

于 2019-06-30T09:47:59.487 回答
0

更新

试试这个

创建两个单独的组件以显示在主页上

ng g c section1
ng g c section2

第 1 节 .ts 文件

@Component({
  selector: 'section1',
  templateUrl: './section1.component.html',
  styleUrls: ['./section1.component.css'],
})

类似 section2 .ts 文件

@Component({
      selector: 'section2',
      templateUrl: './section2.component.html',
      styleUrls: ['./section2.component.css'],
    })

然后在主页上,您可以像这样使用多个部分/组件

页面模板

<div class="row">
  <div class="col-sm-5">
    <section1></section1> <!-- component selector -->
  </div>
  <div class="col-sm-5">
    <section2></section2>
  </div>
</div>
于 2019-06-30T07:43:32.747 回答