如何跨模块共享 utils class
How to share utils class across modules
我有一个 Jhipster 网关应用程序,我想重用嵌套在两个不同父表单中的搜索模式表单。
我已将 ateco-search.component.ts 共享 class 放在 app/shared/model/subjects/utils 下:
app
|_shared
|_model
|_subjects
|_utils
|_ ateco-search.component.ts
|_entities
|_subjects
|_ateco
|_demand
看起来像:
import ...;
@Component({
selector: 'ateco-search-component',
template: `
<div class="modal-header">
...
</div>`
})
export class AtecoSearchComponent {...}
而且我想在ateco模块下调用它。在 ateco-update.component.ts 我有:
import {AtecoSearchComponent} from "app/shared";
@Component({
selector: 'jhi-ateco-update',
templateUrl: './ateco-update.component.html'
})
export class AtecoUpdateComponent implements OnInit {
...
openSearchAtecoModal() {
const modalRef = this.modalService.open(AtecoSearchComponent, {size: 'lg', ariaLabelledBy: 'modal-basic-title'});
...
同样在 demand-update.component.ts 中的需求模块下,我有:
import {AtecoSearchComponent} from "app/shared";
@Component({
selector: 'jhi-demand-update',
templateUrl: './demand-update.component.html'
})
export class DemandUpdateComponent implements OnInit {
...
openSearchAtecoModal() {
const modalRef = this.modalService.open(AtecoSearchComponent, {size: 'lg', ariaLabelledBy: 'modal-basic-title'});
但是当我运行应用程序时,我无法进入ateco-update页面或demand-update页面,得到:
Error: Uncaught (in promise): Error: No NgModule metadata found for 'undefined'.
我应该如何共享 ateco-search.component.ts util class 交叉模块?
创建用于共享组件的 Utils 模块:
utils.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AtecoSearchComponent } from './ateco-search/ateco-search.component';
import { GatewaySharedModule } from 'app/shared';
@NgModule({
declarations: [AtecoSearchComponent],
imports: [GatewaySharedModule, CommonModule],
entryComponents: [AtecoSearchComponent],
exports: [AtecoSearchComponent]
})
export class UtilsModule {}
将它导入到您要使用它的组件的模块中(例如。demand.module.ts)
@NgModule({
imports: [GatewaySharedModule, RouterModule.forChild(ENTITY_STATES), UtilsModule],
...
})
使用共享组件:
const modalRef = this.modalService.open(AtecoSearchComponent, { size: 'lg', ariaLabelledBy: 'modal-basic-title' });
})
我有一个 Jhipster 网关应用程序,我想重用嵌套在两个不同父表单中的搜索模式表单。
我已将 ateco-search.component.ts 共享 class 放在 app/shared/model/subjects/utils 下:
app
|_shared
|_model
|_subjects
|_utils
|_ ateco-search.component.ts
|_entities
|_subjects
|_ateco
|_demand
看起来像:
import ...;
@Component({
selector: 'ateco-search-component',
template: `
<div class="modal-header">
...
</div>`
})
export class AtecoSearchComponent {...}
而且我想在ateco模块下调用它。在 ateco-update.component.ts 我有:
import {AtecoSearchComponent} from "app/shared";
@Component({
selector: 'jhi-ateco-update',
templateUrl: './ateco-update.component.html'
})
export class AtecoUpdateComponent implements OnInit {
...
openSearchAtecoModal() {
const modalRef = this.modalService.open(AtecoSearchComponent, {size: 'lg', ariaLabelledBy: 'modal-basic-title'});
...
同样在 demand-update.component.ts 中的需求模块下,我有:
import {AtecoSearchComponent} from "app/shared";
@Component({
selector: 'jhi-demand-update',
templateUrl: './demand-update.component.html'
})
export class DemandUpdateComponent implements OnInit {
...
openSearchAtecoModal() {
const modalRef = this.modalService.open(AtecoSearchComponent, {size: 'lg', ariaLabelledBy: 'modal-basic-title'});
但是当我运行应用程序时,我无法进入ateco-update页面或demand-update页面,得到:
Error: Uncaught (in promise): Error: No NgModule metadata found for 'undefined'.
我应该如何共享 ateco-search.component.ts util class 交叉模块?
创建用于共享组件的 Utils 模块:
utils.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AtecoSearchComponent } from './ateco-search/ateco-search.component';
import { GatewaySharedModule } from 'app/shared';
@NgModule({
declarations: [AtecoSearchComponent],
imports: [GatewaySharedModule, CommonModule],
entryComponents: [AtecoSearchComponent],
exports: [AtecoSearchComponent]
})
export class UtilsModule {}
将它导入到您要使用它的组件的模块中(例如。demand.module.ts)
@NgModule({
imports: [GatewaySharedModule, RouterModule.forChild(ENTITY_STATES), UtilsModule],
...
})
使用共享组件:
const modalRef = this.modalService.open(AtecoSearchComponent, { size: 'lg', ariaLabelledBy: 'modal-basic-title' });
})