gpt4 book ai didi

javascript - Vue.js - 同类的两个组件无法正确切换

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

我在另一个组件的模板中有两个组件,它们根据点击“回复”和“编辑”按钮进行切换。

<comment-form :model="model" :model-id="modelId" :comment="comment" v-if="showEditForm && canComment" @closeForm="closeForm"></comment-form>
<comment-form :model="model" :model-id="modelId" :parent-id="comment.id" :reply-to="comment" v-if="showReplyForm && canComment" @closeForm="closeForm"></comment-form>

现在的问题是它永远不会出于任何原因关闭第一个打开的实例。当我点击“编辑”然后点击“回复”时,“编辑”的实例似乎保持打开状态,而不是关闭它并打开“回复”实例。

这些是切换表单显示的方法:

methods: {
closeForm: function () {
this.showReplyForm = false;
this.showEditForm = false;
},
reply: function () {
this.showReplyForm = true;
this.showEditForm = false;
},
editComment: function () {
this.showEditForm = true;
this.showReplyForm = false;
},
},

我想了解该行为的原因以及解决方法。

这是整个 comment.vue 文件:

<template>
<div class="comment">
<div class="header">
<div v-if="!comment.user">
<span class="name">{{comment.name}}</span>
wrote on
<span class="time">{{comment.created}}</span>:
</div>
<div v-else>
<span class="name">{{comment.user.username}}</span> wrote on {{comment.created}}:
</div>
</div>
<div class="body">
<template v-for="line in comment.body.split('\n')">{{line}}<br></template>
</div>
<div class="footer">
<a href="#" class="reply-btn" v-on:click.prevent="reply()" v-if="!isLoginRequired || isLoggedIn">
reply
</a>
<span v-if="isMyComment">
&nbsp;&bull;&nbsp;
<a href="#" class="edit-btn" v-on:click.prevent="editComment()">
edit
</a>
&nbsp;&bull;&nbsp;
<a href="#" class="delete-btn" v-on:click.prevent="deleteComment()">
delete
</a>
</span>
</div>
<comment-form :model="model" :model-id="modelId" :comment="comment" v-if="showEditForm && canComment" @closeForm="closeForm"></comment-form>
<comment-form :model="model" :model-id="modelId" :parent-id="comment.id" :reply-to="comment" v-if="showReplyForm && canComment" @closeForm="closeForm"></comment-form>
<comments-list :level="level + 1" v-if="hasChildren" :model="model" :model-id="modelId" :parent-id="comment.id"></comments-list>
</div>
</template>

<script>
export default {
props: {
comment: null,
modelId: null,
level: null,
parentId: null,
model: {
type: String,
default: null
},
},
computed: {
hasChildren: function() {
return this.$commentsStore.getters.hasChildren(
this.model,
this.modelId,
this.comment.id,
);
},
canComment: function() {
return this.$commentsStore.getters.canPost;
},
isLoggedIn: function() {
return this.$commentsStore.getters.isLoggedIn;
},
isMyComment: function () {
return this.$commentsStore.getters.isMyComment(this.comment);
},
isLoginRequired: function() {
return this.$commentsStore.getters.getConfig('loginRequired');
}
},
methods: {
closeForm: function () {
this.showReplyForm = false;
this.showEditForm = false;
},
reply: function () {
this.showReplyForm = true;
this.showEditForm = false;
},
editComment: function () {
this.showEditForm = true;
this.showReplyForm = false;
},
deleteComment: function () {
return this.$commentsStore.dispatch('deleteComment', this.comment);
}
},
data: function() {
return {
showReplyForm: false,
showEditForm: false
};
}
};
</script>

最佳答案

这有点疯狂,但你需要一个 key .这是我见过的第一个在 v-for 之外需要的示例。

发生的事情是您在两个 v-if 中拥有相同的组件。当 Vue 开始更新时,它发现它应该有一个 comment-form,而且它已经有了。它不承认差异。我将其称为 Vue 中的错误,但解决方法是为每个组件实例提供一个 key

new Vue({
el: '#app',
data: {
showEditForm: true,
showReplyForm: false
},
components: {
commentForm: {
methods: {
close() {
this.$emit('close-form');
}
}
}
},
methods: {
closeForm: function() {
this.showReplyForm = false;
this.showEditForm = false;
},
reply: function() {
this.showReplyForm = true;
this.showEditForm = false;
},
editComment: function() {
this.showEditForm = true;
this.showReplyForm = false;
},
}
})
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<div id="app">
<button @click="reply">
Reply
</button>
<button @click="editComment">
Edit
</button>
<comment-form v-if="showEditForm" key="edit" @close-form="closeForm" inline-template>
<div>
This is the edit form
<button @click="close">
Close it
</button>
</div>
</comment-form>
<comment-form id="reply" key="reply" v-if="showReplyForm" @close-form="closeForm" inline-template>
<div>
This is the reply form
<button @click="close">
Close it
</button>
</div>
</comment-form>
</div>

关于javascript - Vue.js - 同类的两个组件无法正确切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48462639/

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