gpt4 book ai didi

javascript - 嵌套组件未正确重新呈现 : VueJs

转载 作者:搜寻专家 更新时间:2023-10-30 22:42:00 24 4
gpt4 key购买 nike

我是 Vue 的新手,我正在构建这种可以在其中添加嵌套评论的论坛。这里有两个组件。发布论坛和评论。 PostForum 包含一个输入框和父 Comments。在每条评论中,我递归地添加了子评论。

当我添加评论时,它工作正常。但是在删除时,它会发送 ajax req 但没有重新渲染。所以我就是这样设计的。删除评论时,我会发出一个全局事件,并在 PostForum 组件中监听该事件并从其数据中删除该评论。那么难道不应该相应地重新呈现所有评论吗?谁能告诉我我在这里做错了什么?

PostForum.vue

<template>
<!-- comment box here -->

<comment
v-for="(comment, index) in comments"
v-if="!comment.parent_id"
:reply="true"
:initialChildren="getChildren(comment.id)"
:key="index"
:comment="comment">
</comment>
</template>

<script>
export default {
data () {
return {
comments: [], // all comments
comment: { // new comment [at comment box]
body: '',
parent_id: 0,
},
}
},
methods: {
deleteComment (node) {
axios.delete(`/comments/${node.id}`)
.then(res => {
this.comments.splice(node.key, 1)
})
.catch(err => {
console.log(err)
})
},
getChildren: function (parent_id) {
return this.comments.filter(child => parent_id == child.parent_id)
},
},
mounted: function () {
window.Event.$on('comment-deleted', (node) => this.deleteComment(node))
}
}
</script>

评论.vue

<template>
<button @click="deleteComment">X</button>

<!-- comment body goes here -->

<comment v-for="(child, i) in children" :key="i" :reply="false" :comment="child"></comment>

<!-- reply form here -->
</template>

<script>
export default {
props: ['initialChildren']
data: function () {
return {
newComment: {
body: '',
parent_id: this.comment.id,
},
children: this.initialChildren,
}
},
methods: {
deleteComment () {
window.Event.$emit('comment-deleted', {key: this.$vnode.key, id: this.comment.id})
},
}
}
</script>

最佳答案

我已经试过了:

此代码只是一个可能对您有所帮助的示例。在我的例子中,child 组件是你的 comment 组件,每个 child 组件都有自己的 @action他的 child 组件的监听器。因此,他可以使用它来修改自己的 children

这里是codesandbox的例子:https://codesandbox.io/s/qzrp4p3qw9

父组件

<template>
<div>
<Child v-for="(children,index) in childrens" :child="children" :key="index" :parent="0" :pos="index"></Child>
</div>
</template>
import Child from './child'; 
export default {
data() {
return {
childrens:[
{
name:"a",
childrens:[
{
name:'aa',
},
{
name:'ba',
childrens:[
{
name:'baa',
childrens:[
{
name:'baaa',
},
{
name:'baab',
}
]
}
]
}
]
},
{
name:"a",
childrens:[
{
name:'aa',
},
{
name:'ab',
childrens:[
{
name:'aba',
childrens:[
{
name:'abaa',
childrens:[
{
name:'baa',
childrens:[
{
name:'baaa',
},
{
name:'baa',
}
]
}
]
},
{
name:'abab',
}
]
}
]
}
]
}
]
}
},
components:{
Child
}
}

子组件

<template>
<div>
<div style="padding:5px">
{{ child.name }}
<button @click="deleteComment(child)">x</button>
</div>
<child @delete="deleteSubComment" style="padding-left:15px" v-if="typeof child.childrens !== 'undefined'" v-for="(children,index) in child.childrens" :child="children" :pos="index" :key="index" :parent="children.parent"></child>
</div>
</template>
export default {
name:"child",
props:['child','parent',"pos"],
methods:{
deleteComment(child) {
this.$emit('delete',child);
},
deleteSubComment(obj) {

this.child.childrens.splice(this.child.childrens.indexOf(obj),1);
}
}
}

关于javascript - 嵌套组件未正确重新呈现 : VueJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52551840/

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