我正在创建聊天体验,并且页面通过使用 ionic-angular 上的 Content 中的 scrollToBottom 方法来跟上对话。如果我从“PreChat.html”开始并转到“Chat.html”,然后我关闭“Chat.html”以返回“PreChat.html”并再次返回“Chat.html”,我会收到以下错误: “无法读取 null 的属性‘scrollToBottom’”。经验应该可以随时输入或重复输入。
PreChat.html:
<ion-content>
<button (click)="goToChat()" href="#">
Chat
</button>
</ion-content>
PreChat.ts:
import { Component } from '@angular/core';
import { ModalController } from 'ionic-angular';
import { Chat } from '../chat';
@Component({
selector: 'preChat',
templateUrl: 'preChat.html'
})
export class PreChat {
constructor(private modalCtrl: ModalController){}
goToChat(): any {
let profileModal = this.modalCtrl.create(Chat);
profileModal.present();
}
}
聊天.html:
<ion-content>
<button ion-button="filter-button"(click)="dismiss()">
<ion-icon name="close"></ion-icon>
</button>
<div *ngFor="let convo of conversationArray">
<div class="b-text">{{convo.speak}}</div>
</div>
</ion-content>
聊天记录:
import { Component, ViewChild } from '@angular/core';
import { Content, Events, ViewController } from 'ionic-angular';
@Component({
templateUrl: 'Chat.html'
})
export class Chat {
@ViewChild(Content) content: Content;
conversationArray: any[] = [];
constructor(private events: Events, private viewCtrl: ViewController){
this.events.subscribe('conversation', convo => {
this.conversationArray = convo;
this.content.scrollToBottom();
})
}
dismiss() {
this.viewCtrl.dismiss();
}
}
最佳答案
为此,您必须使用 ionViewDidEnter
页面事件而不是 constructor
。
ionViewDidEnter() : void {
this.platform.ready().then(() => { //platform ready
this.events.subscribe('conversation', convo => {
this.conversationArray = convo;
this.content.scrollToBottom();
})
})
}
关于javascript - ionic 2 : How do you reinstantiate @ViewChild if you dismiss the view?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43511090/