我的 ionic 项目中的 NavParams 遇到了一些奇怪的问题。我能够将它从第一页传递到第二页,但是当试图将它从第二页传递到第三页时,它是未定义的。
主页.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FirebaseService } from './../../providers/firebase-service/firebase-service';
import { Post } from '../../models/post/post.model';
import { Observable } from 'rxjs/Observable';
import { IonicPage } from 'ionic-angular/navigation/ionic-page';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireObject } from 'angularfire2/database/interfaces';
import { User } from '../../models/user/user.model';
import firebase from 'firebase';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
userData: AngularFireObject<User>;
posts$: Observable<Post[]>;
checkRole: boolean = true;
constructor(public navCtrl: NavController, private firebaseService: FirebaseService, private afAuth: AngularFireAuth) {
this.posts$ = this.firebaseService
.getPosts() // DB LIST
.snapshotChanges() // Key and Value
.map(changes => {
return changes.map(c => ({
key: c.payload.key, ...c.payload.val()
}))
});
}
viewPost(post: Post) {
this.navCtrl.setRoot('PostPage', { post: post });
}
}
post.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Post } from '../../models/post/post.model';
@IonicPage()
@Component({
selector: 'page-post',
templateUrl: 'post.html',
})
export class PostPage {
post: Post;
constructor(public navCtrl: NavController, public navParams: NavParams, private afAuth: AngularFireAuth) {
this.post = this.navParams.get('post');
}
ionViewDidLoad() {
console.log(this.post);
}
editPost(post: Post) {
this.navCtrl.push('EditPostPage', { post: post });
}
}
在第 2 页上一切都很好,控制台日志显示了所有正确的信息。
编辑-post.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Post } from '../../models/post/post.model';
import { FirebaseService } from '../../providers/firebase-service/firebase-service';
@IonicPage()
@Component({
selector: 'page-edit-post',
templateUrl: 'edit-post.html',
})
export class EditPostPage {
post: Post;
constructor(public navCtrl: NavController, public navParams: NavParams, private firebaseService: FirebaseService) {
this.post = this.navParams.get("post");
}
ionViewDidLoad() {
console.log(this.post);
}
savePost(post: Post) {
this.firebaseService.editPost(post).then(() => {
this.navCtrl.pop();
});
}
}
这就是它变得奇怪的地方。我在第 3 页的 this.item 上未定义
- 节点 v.8.94
- 科尔多瓦@8.0.0
- 离子@3.19.1
这是一个错误吗?请帮忙。