未找到自定义的ionic2组分

时间:2016-11-22 12:31:22

标签: ionic-framework components ionic2

我有一个包含此代码的登录组件:

import { Injectable, Component } from '@angular/core';
import { Validators, FormBuilder } from '@angular/forms';

@Component({
  selector: 'login',
  templateUrl: 'login.html'
})

@Injectable()
export class LoginComponent {
  login: any;
  text: string;

  constructor(private formBuilder: FormBuilder) {
    this.text = 'Hello World';
  }

  ionViewLoaded() {
    this.login = this.formBuilder.group({
      title: ['', Validators.required],
      description: [''],
    });
  }

  doLogin(){
    console.log(this.login.value)
  }
}

我尝试使用

从页面的打字稿文件中调用它
import { LoginComponent } from '../../component/login/login';

export class QuestionsPage {

  constructor(public navCtrl: NavController, public loginComponent: LoginComponent ) {

  }

结果是:

  

[13:23:46] typescript:src / pages / questions / questions.ts,line:17               找不到名称'LoginComponent'。

这是“构造函数(公共......”行。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否已将组件添加到@NgModules声明数组?

src/app/app.component.ts中,按照以下方式添加:

import { LoginComponent } from '../../component/login/login';

@NgModule({
  declarations: [
    ..
    LoginComponent
    ..
  ]
})

您可以在此处阅读更多https://github.com/driftyco/ionic/blob/master/CHANGELOG.md#copying-your-project-to-a-new-project和此处 https://angular.io/docs/ts/latest/guide/ngmodule.html