【问题标题】:Angular share navbarComponent to another ComponentAngular 将导航栏组件共享到另一个组件
【发布时间】:2020-03-30 16:18:33
【问题描述】:

我在问如何与仪表板组件和仪表板组件内的所有组件(例如课程组件)共享我的导航菜单仪表板组件(在共享/布局内),并且我有一个用于带有路由的仪表板组件的模块。

仪表板模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { DashboardRoutingModule } from './dashboard-routing.module';
import { CoursesComponent } from './courses/courses.component';


@NgModule({
declarations: [CoursesComponent],
imports: [
CommonModule,
DashboardRoutingModule
]
})
export class DashboardModule { }

我在 AppModule 中声明了 DashboardModule:

【问题讨论】:

  • 你能分享一下stackblitz吗?
  • 有 nav-menu-dashboard 模块吗?
  • 我在 appModule 中声明的导航菜单仪表板没有模块
  • 您可以创建一个模块 nav-menu-dashboard 并导入到仪表板模块,之后您只需在仪表板文件夹中导入您需要的组件。
  • 谢谢您的回答...但我需要将导航菜单仪表板放在dashboardComponent.html中,仪表板文件夹中的所有文件夹都使用这个导航...就像我把它放在appComponent.html 整个应用程序将采用 nav-menu-dashboard 组件

标签: angular navbar file-structure


【解决方案1】:

您可以在 DashboardComponent.html 中执行以下操作

<nav-menu-dashboard #navigation></nav-menu-dashboard>

并在 DashboardComponent.ts 中添加以下内容:

@ViewChild('navigation', {static: false}) navigationComponent: NavMenuDashboardComponent;

您可以通过实现 ViewChild 方法来访问 navigationComponent 中的方法和其他属性。

【讨论】:

  • 感谢您的回答..但是什么是 naviagtionComponent 以及如何在仪表板组件中显示 nav-menu-dashboard 以及在仪表板文件夹中显示整个文件夹
  • 'navigation' 是对 NavMenuDashboard 的引用(指令)-> 在 html 中声明。 “navigationComponent”只是指向您的 NavMenuDashboardComponent 的变量声明。当您说您需要仪表板内的整个文件夹时,您是什么意思?可能我不明白你的问题。您拥有的共享文件夹是正确的,您要使用的子组件应在父模块中声明,在您的情况下为仪表板模块,而不是在其他位置(应用程序模块)。如果出现重复。我认为您需要重新考虑 DashBoard 模块
猜你喜欢
  • 2018-05-31
  • 2018-05-31
  • 2018-07-21
  • 2019-09-10
  • 1970-01-01
  • 2017-06-07
  • 2021-01-03
  • 2019-08-20
  • 1970-01-01
相关资源
最近更新 更多