gpt4 book ai didi

javascript - ionic 2 : How do you reinstantiate @ViewChild if you dismiss the view?

转载 作者:太空狗 更新时间:2023-10-29 18:20:21 27 4
gpt4 key购买 nike

我正在创建一种聊天体验,并且该页面通过使用 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>

聊天.ts:

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

Runs when the page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page.

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com