【问题标题】:Self defined ionic2 component is not found未找到自定义的ionic2组件
【发布时间】:2016-11-22 12:31:22
【问题描述】:

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

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] 打字稿:src/pages/questions/questions.ts,行:17 找不到名称“LoginComponent”。

这是“constructor(public ...”行。

我做错了什么?

【问题讨论】:

    标签: ionic-framework components ionic2


    【解决方案1】:

    您是否将组件添加到 @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

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 2017-05-02
      • 1970-01-01
      • 2013-12-23
      • 2013-01-10
      • 1970-01-01
      • 2018-01-10
      • 2017-09-07
      • 2017-08-22
      相关资源
      最近更新 更多