[英]Vue live update props array in axios callback
这次它是一般而简单的,所以我没有真正提供代码。 一般步骤是:
这里有一些代码:
<div class="users" v-for="participant in part" :key="participant.id">
<template v-if="participant.name !== username">
{{participant.name}}
<span>
<a style="cursor:pointer" title="kick" @click="kickUser(participant)">
...
props: ["participants", "username", "channel"],
methods: {
kickUser(user) {
axios
.post("/kickuser", { user: user, channel: this.channel })
// .then((this.participants = []));
}
kickuser axios post 方法从数据库中删除了一个用户,因此该数组被用户踢了,希望你能帮助我计算属性
在这里做一些假设,所以我可能需要在信息曝光时对其进行编辑......
您有一个包含数据的父组件。 例如
data: () => ({ participants: [...] }),
然后将该数据传递给子组件
<Child :participants="participants"
:username="..." :channel="..."/>
在该子组件中,您执行涉及发出 HTTP 请求的操作。
然后你要做的是从子组件发出一个事件
methods: {
async kickUser (user) {
let { data } = await axios.post('/kickuser', { user, channel: this.channel })
this.$emit('kick', data)
}
}
并在父级中监听此事件
<Child :participants="participants" @kick="handleKick"
:username="..." :channel="..."/>
// parent "methods"
methods: {
handleKick (data) {
this.participants = [] // or whatever you need to do
}
}
这个过程在 Vue 的文档中有所概述 ~ 单向数据流
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.