Ionic3 Lazy Loading已导入意外指令

时间:2017-05-04 08:04:26

标签: angular ionic-framework ionic2 ionic3

我正在使用Ionic3,并且正在实施延迟加载以提高启动性能。

我转换了以下内容:

loginemail.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { LoginEmailPage } from './loginemail';
import { ControlMessages } from '../validation/controlMessages';

@NgModule({
  declarations: [LoginEmailPage],
  imports: [IonicPageModule.forChild(LoginEmailPage), ControlMessages],
})
export class LoginEmailPageModule { }

如您所见,我导入ControlMessages,这是一个自定义组件(与app.module.ts中导入的预先加载完美配合)。

但是,当我尝试访问LoginEmailPage时,我收到以下运行时错误:

core.es5.js:1085 ERROR Error: Uncaught (in promise): Error: Unexpected directive 'ControlMessages' imported by the module 'LoginEmailPageModule'. Please add a @NgModule annotation.
Error: Unexpected directive 'ControlMessages' imported by the module 'LoginEmailPageModule'. Please add a @NgModule annotation.

任何建议都表示赞赏。

P.S。对于使用它的其他网页,ControlMessages仍会导入app.module.ts

controlMessages.ts

import { Component, Input } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ValidationService } from './validationService';

@Component({
  selector: 'control-messages',
  template: `<div *ngIf="errorMessage !== null">{{errorMessage}}</div>`
})
export class ControlMessages {
  @Input() control: FormControl;

  constructor() {

  }

  get errorMessage(): string {
    for (let propertyName in this.control.errors) {
      if (this.control.errors.hasOwnProperty(propertyName) && this.control.touched) {
        return ValidationService.getValidatorErrorMessage(propertyName, this.control.errors[propertyName]);
      }
    }

    return null;
  }
}

1 个答案:

答案 0 :(得分:1)

需要创建一个ControlMessagesModule,而不是导入它。

import { NgModule } from '@angular/core';
import { IonicModule } from 'ionic-angular';
import { CommonModule } from '@angular/common';
import { ControlMessages } from './controlMessages';

@NgModule({
  declarations: [ControlMessages],
  imports: [
    CommonModule,
    IonicModule
  ],
  exports: [ControlMessages]
})
export class ControlMessagesModule { } 

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { LoginEmailPage } from './loginemail';
import { ControlMessagesModule } from '../validation/controlMessages.module';

@NgModule({
  declarations: [LoginEmailPage],
  imports: [IonicPageModule.forChild(LoginEmailPage), ControlMessagesModule],
  exports: [LoginEmailPage]
})
export class LoginEmailPageModule { }