简体   繁体   English

在各种组件中调用一个组件。 角度4

[英]Call one component in various components. Angular 4

Hello im using Angular 4. I have a component with some html that i want it to be showned in various components. 您好,我使用Angular4。我有一个带有一些html的组件,希望将其显示在各个组件中。 But when i import my module into various others moduels it says that it only can be defined in one module... how can i make it work? 但是,当我将我的模块导入其他各种模块时,它说只能在一个模块中定义...我如何使它工作?

i want param-var to be showned in globales and resumen. 我希望param-var在全局变量中显示并恢复。

Console Error 控制台错误

ERROR Error: Uncaught (in promise): Error: Type ParamVarComponent is part of the declarations of 2 modules: GlobalesModule and ResumenModule! 错误错误:未捕获(承诺):错误:类型ParamVarComponent是以下两个模块的声明的一部分:GlobalesModule和ResumenModule! Please consider moving ParamVarComponent to a higher module that imports GlobalesModule and ResumenModule. 请考虑将ParamVarComponent移至导入GlobalesModule和ResumenModule的更高模块。 You can also create a new NgModule that exports and includes ParamVarComponent then import that NgModule in GlobalesModule and ResumenModule. 您还可以创建一个新的NgModule,该导出并包含ParamVarComponent,然后在GlobalesModule和ResumenModule中导入该NgModule。

PARAM VAR MODULE 参数VAR模块

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


import { ParamVarRoutingModule } from './param-var-routing.module';
import { ParamVarComponent } from './param-var.component';

import { FormsModule, ReactiveFormsModule} from '@angular/forms';

@NgModule({
  imports: [
    CommonModule,
    ParamVarRoutingModule,
    FormsModule,
    ReactiveFormsModule
  ],
  declarations: [ParamVarComponent,

  ]
})
export class ParamVarModule { }

GLOBALES MODULE 全球模块

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


import { GlobalesRoutingModule } from './globales-routing.module';
import { GlobalesComponent } from './globales.component';

import {ParamVarComponent} from '../param-var/param-var.component';
import { FormsModule, ReactiveFormsModule} from '@angular/forms';

@NgModule({
  imports: [
    CommonModule,
    GlobalesRoutingModule,
    FormsModule,
    ReactiveFormsModule,

  ],
  declarations: [GlobalesComponent,ParamVarComponent,


  ]
})
export class GlobalesModule { }

RESUMEN MODULE 简历模块

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


import { ResumenRoutingModule } from './resumen-routing.module';
import { ResumenComponent } from './resumen.component';

import { FormsModule, ReactiveFormsModule} from '@angular/forms';
import {ParamVarComponent} from '../param-var/param-var.component';

@NgModule({
  imports: [
    CommonModule,
    ResumenRoutingModule,
    FormsModule,
    ReactiveFormsModule,

  ],
  declarations: [ResumenComponent,ParamVarComponent,


  ]
})
export class ResumenModule { }

Create new SharedModule (or any other name, but that one matches Angular's Style Guide). 创建新的SharedModule (或任何其他名称,但要与Angular的样式指南匹配)。 Declare and export your ParamVarComponent there. ParamVarComponent声明并导出您的ParamVarComponent Now you can import SharedModule in any other module in your application as many times as you want. 现在,您可以根据需要多次在应用程序的任何其他模块中导入SharedModule Since ParamVarComponent is exported, you can use it inside of other modules as soon as those modules import SharedModule . 由于已导出ParamVarComponent ,因此只要这些模块导入SharedModule ,便可以在其他模块中使用它。

@NgModule({
     imports: [
         CommonModule,
         FormsModule,
         ReactiveFormsModule
    ],
    declarations: [ParamVarComponent],
    exports: [ParamVarComponent]
})
export class SharedModule { }

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        SharedModule 
    ]
})
export class OtherModule#{ }

暂无
暂无

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

相关问题 在各种组件中显示一个组件的相同数据。 角度4 - show the same data of one component in various components. Angular 4 在一个组件中导入 CSS 正在将 CSS 影响添加到其他组件。 如何和为什么? - Importing CSS in one component is adding CSS affect to other Components. How and Why? 我有两个组成部分。 我想将第一个组件称为第二个组件。 我该怎么做? - I have two components. I want to call 1st component into the second component. How can I do it? 角度2-我有3个标头组件。 如何最好地切换这2个标题? - Angular 2 - I have 3 headers components. Hows best to toggle these 2 headers? 如何为所有组件创建全局预加载器。 角度2 - How to create global preloader to all components. Angular 2 多个角度5个组件操纵一个中心组件 - Multiple angular 5 components manipulate one central component 停止重新渲染父组件。 仅重新渲染子组件 - Stop Re-Rendering of Parent Components. Only Re-Render Child Component React 抽象组件 (ES6) 呈现组件数组。 如何为它创建一个 d.ts 文件? - React abstract component (ES6) that renders an array of components. How to create a d.ts file for it? 使用一个主要的 class 具有我在组件中需要的所有类。 那是坏方法吗? - Use one main class has all classes that I need in my components. Is that bad way? 从组件 Angular 调用一个组件的函数 - Call function from one component from a component Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM