【发布时间】:2018-01-02 21:35:31
【问题描述】:
我正在尝试使用 EventEmitter 和输出将数据从我的 home.ts 文件发送到 app.component.ts 文件。但是每次我在 app.html 中引用主页组件时,都会出现这个看似随机的错误。当我从 home.ts 的构造函数中删除 NavController 时,错误消失了。
home.ts:
import { Component, EventEmitter, Output } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
message : any;
@Output() notify : EventEmitter<Object> = new EventEmitter<Object>();
constructor(public navCtrl: NavController) {
}
ionViewDidLoad(){
this.message = {"Name":"Sid", "Age":17};
this.notify.emit(this.message);
}
}
app.html:
<ion-nav [root]="rootPage"></ion-nav>
<page-home (notify)="getChildData($event)"></page-home>
app.component.ts :
import { Component, ViewChild, ViewChildren } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen:
SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});
}
getChildData(message){
console.log(message["Name"]);
console.log(message["Age"]);
}
}
如何解决此错误?我需要使用 NavController 所以我不能删除它。我希望仍然能够将数据从子组件发送到父组件
【问题讨论】:
标签: html angular typescript ionic2