vue.js - 如何在 v-for nativescript vue 内部刷新
问题描述
我正在尝试为我的应用程序制作聊天功能。为了显示消息,我对所有消息都有一个 v-for 循环。但是当我添加一条消息时,它不会显示在页面上。
在模板中使用 v-for 模型,如下所示。
<WrapLayout v-for="(message, index) of getMessages" v-bind:key="index">
<StackLayout class="chat-box-content">
<Label
class="text-content body"
textWrap="true"
:text="message.message"
/>
</StackLayout>
</WrapLayout>
在 v-for 模型中,我从计算方法 getMessages() 中获取消息。添加消息时,会显示控制台日志“已添加消息”。所以计算方法看到添加了一条消息,但它没有显示在我的页面上。
computed: {
getMessages() {
let messages;
let chats = store.getters.getChats;
chats.forEach((element) => {
if (element.chatId === this.chatId) {
console.log("message added")
messages = element.messages;
}
});
return messages;
},
我从 store.ts 中收到消息。消息是聊天的一部分。
export interface IChat {
chatId: string,
lastMessage: string,
timestamp: string,
senderName: string,
members: [{
userId: string,
name: string
}],
messages: [{
message: string,
sender: string,
timestamp: string,
}],
}
更新:
所以显然我的 v-for 在添加消息时会重新加载,但不是在我第一次访问页面时。
- 我第一次启动应用程序,导航到我的聊天,发送消息,消息没有显示在屏幕上。
- 我单击后退按钮,再次导航到聊天,发送新消息,消息现在显示在屏幕上。
有人知道问题可能是什么吗?
解决方案
您能否检查计算属性getMassages
是否具有您期望的结构?看起来你假设它
[{message:"hello"}, ...]
是正确的?请添加更多代码以提供更好的上下文。
推荐阅读
- google-cloud-platform - 需要 GCP 计费支持 - 服务已停止但仍需收费
- vba - 根据检测到的 Word 版本在 Word 功能区上显示自定义选项卡?
- android - 如何通过 Kotlin 中的数据绑定设置 Android 复合视图的可见性?
- javascript - javascript停止工作但在不同的服务器上工作
- javascript - 将对象添加到数组 onclick Javascript
- c# - 迁移时出错 - 数据库中已有一个名为“AspNetRoles”的对象。(实体框架核心)
- php - 查询生成器中的 CodeIgniter 子查询
- c++ - 我想知道是否可以将一个类分配给特定的成员数据并创建该类的实例
- python-3.x - Python3:如何在后台运行 localhost?
- amazon-web-services - 无法解析私有 DNS,但能够连接到 IP 地址