Как создать собственный глобальный модуль - PullRequest
0 голосов
/ 26 мая 2020

Я хочу создать модуль, в котором я объявляю и обновляю все ngx-formly вещи, чтобы импортировать этот модуль в другие модули как глобальный модуль. Но у меня есть ошибки в компонентах, которые являются компонентом ngx-formly.

formly.module.ts

import { FormlyPassword } from './../shared/formly-custom-templates/formly-password.type';
import { NgModule } from '@angular/core';
import { FormlyNgZorroAntdModule } from '@ngx-formly/ng-zorro-antd';



@NgModule({
  declarations: [FormlyPassword],
  imports: [
    FormlyModule,
    FormlyNgZorroAntdModule,
    FormlyModule.forChild({
      types: [
        { name: 'custom-input', component: FormlyPassword },
      ]
    }),
  ],
})
export class NgFormlyModule { }

core.module.ts

import { NgFormlyModule } from './../formly/formly.module';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './components/login/login.component';
import { ForgetPasswordComponent } from './components/forget-password/forget-password.component';
import {CoreRoutingModule} from './core-routing.module';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {ZorroModule} from '../zorro/zorro.module';

import {SharedModule} from '../shared/shared.module';



@NgModule({
  declarations: [LoginComponent, ForgetPasswordComponent],
  imports: [
    CommonModule,
    CoreRoutingModule,
    SharedModule,
    FormsModule,
    ReactiveFormsModule,
    ZorroModule,
    NgFormlyModule // here is imported custom module
  ]
})
export class CoreModule { }

ошибка

    ERROR in src/app/core/components/login/login.component.html:44:9 - error NG8001: 'formly-form' is not a known element:
    1. If 'formly-form' is an Angular component, then verify that it is part of this module.
    2. If 'formly-form' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

    44         <formly-form [model]="model" [fields]="fields">
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      src/app/core/components/login/login.component.ts:7:18
        7     templateUrl: './login.component.html',
                           ~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component LoginComponent.

1 Ответ

0 голосов
/ 26 мая 2020

Вы должны экспортировать все компоненты, которые хотите использовать в модуле, который импортирует ваш пользовательский модуль:

@NgModule({
declarations: [FormlyPassword],
imports: [
 FormlyModule,
 FormlyNgZorroAntdModule,
 FormlyModule.forChild({
  types: [
    { name: 'custom-input', component: FormlyPassword },
     ]
   }),
 ],
exports: [FormlyPassword]

})
export class NgFormlyModule { }

Тогда все модули, которые импортируют ваш NgFormlyModule, смогут использовать FormlyPassword.

...