javascript - 使用 angular-router 在页面中导航 View

标签 javascript angular angular7 angular-routing angular7-router

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

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

这是我的一般设置。

应用模板

<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(){} }

最佳答案

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

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

检查一下:Stackblitz只是供您使用的示例。

app.module 中有一个组件,intmodule 中有一个组件,intmodule 中有两个组件。 从 app.module 我们可以在 hello.componentintmodule 之间路由,在 intmodule 中我们可以在 comp1 之间路由和 comp2。

评论以获得更多帮助:)

关于javascript - 使用 angular-router 在页面中导航 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823159/

相关文章:

javascript - 我应该如何访问react-redux应用程序中的服务

html - 无法打印整个 div,包括(X 符号开头的一些删除按钮和同一行中的文件名等)

angular - 选择复选框 Angular 中的所有项目

html - 没有 ngmodel 指令 Angular 两种方式数据绑定(bind)

versioning - 自定义 PWA 版本管理

javascript - 以一定速度滚动时,Masonry 和 Infinite Scroll 会破坏布局

javascript - 使完美的滚动条默认可见

javascript - 在 Javascript 中三次错误尝试后触发重定向到表单提交页面

java - 如何通过 angularJS 将 java 对象发送到 get 方法

angular - 在文件 :///without server 上本地运行 Angular 7 项目