简体   繁体   English

Angular Shared Module导入不会导入提到的模块

[英]Angular Shared Module import does not import mentioned modules

I have added shared module to my app. 我已将共享模块添加到我的应用程序。 It looks pretty simple. 看起来很简单。

const imports = [
    CommonModule,
    RouterModule,
    BrowserModule,
    FormsModule, ReactiveFormsModule
];


@NgModule({imports})

export class SharedModule {}

When I import my shared module into another module it does not see my imported staff. 当我将共享模块导入另一个模块时,看不到导入的人员。 I get error like this "Can't bind to 'formGroup' since it isn't a known property of 'form'. (" ,which means that FormsModule is not imported correctly. If I import that basic modules directly into my app module it works OK. 我收到这样的错误:“无法绑定到'formGroup',因为它不是'form'的已知属性。(”,这意味着FormsModule无法正确导入。如果我将基本模块直接导入到我的应用模块中它工作正常。

Your help and advises are highly appreciated. 非常感谢您的帮助和建议。

Most likely you will need to also export those modules. 您很可能还需要导出这些模块。

Typically: 典型:

@NgModule({
    imports: [
        SomeModule
    ],
    declarations: [
        ...
    ],
    exports: [
        SomeModule,
    ]
});

Also see this angular doco... 另请参阅此角形文档...

By re-exporting CommonModule and FormsModule, any other module that imports this SharedModule, gets access to directives like NgIf and NgFor from CommonModule and can bind to component properties with [(ngModel)], a directive in the FormsModule. 通过重新导出 CommonModule和FormsModule,导入此SharedModule的任何其他模块都可以从CommonModule访问NgIf和NgFor之类的指令,并且可以使用FormsModule中的指令[[ngModel)]绑定到组件属性。

You have to export the dependency modules which used inside the shared module in order to use the shared module from some where else which does not import those dependencies. 您必须导出在共享模块中使用的依赖模块,以便从其他不导入这些依赖关系的地方使用共享模块。

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CustomerComponent } from './customer.component';

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

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

相关问题 共享模块中的角度导入/导出模块或多个模块中的导入 - Angular Import/Export modules in shared Module or Import in Multiple Modules 花时间将共享模块导入Angular的延迟加载模块中 - Taking time to import shared module into lazy loaded modules in angular 如何使用ModuleWithProviders导入Angular模块中的其他模块 - How to import other modules in Angular Module with ModuleWithProviders 如何通过 Angular 4 中的共享模块正确导入 Angular Material 模块? - How to correctly import the Angular Material module through a shared module in Angular 4? 我应该以什么顺序在共享模块中导入模块 - In what order should i import modules in my shared module 核心模块是否应该在 Angular2 中导入共享模块 - Should the core module import the shared module in Angular2 如何判断Angular2模块是否可以导入共享模块 - How to tell if an Angular2 module is ok to import into shared module 将全局使用的库模块导入angular个工程模块 - Import globally used library module into angular project modules Angular 7.如何导入此共享模块? - Angular 7. How can I import this shared module? 开玩笑 - 找不到具有从共享(Angular)相对导入的模块 - Jest - cannot find module with relative import from shared (Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM