我試圖使用EventEmitter和Output將數據從我的home.ts文件發送到app.component.ts文件。但每次我在我的app.html中引用主頁組件時,都會看到這個看似隨機的錯誤。當我從home.ts中的構造函數中刪除NavController時,錯誤消失了。嘗試將數據從子組件傳遞到父組件時出現「沒有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,所以我不能刪除它。我想仍然能夠將數據從子組件發送到父組件
參考https://forum.ionicframework.com/t/why-cant-i-import-navcontroller-and-viewcontroller-into-service-or-app/40999/46 – monica