javascript - 在 v-for 内的 vue js 中编辑状态

标签 javascript vue.js vuejs2

我正在从 api 获取评论。我正在尝试编辑列表中的评论。

当我单击“编辑”时,我会在评论框中显示一个文本区域以及一个用于保存评论的按钮。效果很好。

问题是当我单击“编辑”时,编辑文本区域会应用于所有评论框:|

         <div>
                    <i class="fas fa-pen text-action text-success mr-3" @click="showComment(comment.id)" v-if="user == comment.user_id" v-b-tooltip.hover title="edit"></i>
                    <i class="fas fa-times-circle text-danger" v-b-tooltip.hover title="delete" v-if="!editing" v-show="user == comment.user_id"></i>
                    <i class="fa fa-times text-action text-danger mr-3" v-if="editing" @click="editing = false" v-b-tooltip.hover title="cancel editing"></i>
                </div>
            </div>
            <div v-if="editing">
                <textarea class="form-control" v-model="commentEdited" :id="comment.id" :placeholder="comment.body"></textarea>
                <button class="btn btn-xs btn-success" @click="editComment(comment)">save</button>
            </div>


显示评论方法:

showComment(comment_id) {
    console.log("clicked" + comment_id);
    for (let i = 0; i < this.comments.length; i++) {
        if (this.comments[i].id == comment_id) {
            this.comment.body = this.comments[i].body;
            this.comment.id = this.comments[i].id;
            this.editing = true;
        }
    }
},

编辑是一个 bool 值。

当我单击“编辑”时,它应该只打开当前评论的文本区域。并非所有评论:\

我真的无法弄清楚这一点。非常感谢任何帮助。

最佳答案

这是因为所有组件都使用 editing bool 值,因此当将其设置为 true 时,所有注释都会显示。您可以将编辑更改为等于正在编辑的评论的 id 的字符串属性:

        <div>
                <i class="fas fa-pen text-action text-success mr-3" @click="showComment(comment.id)" v-if="user == comment.user_id" v-b-tooltip.hover title="edit"></i>
                <i class="fas fa-times-circle text-danger" v-b-tooltip.hover title="delete" v-if="!editing" v-show="user == comment.user_id"></i>
                <i class="fa fa-times text-action text-danger mr-3" v-if="editing" @click="editing = ''" v-b-tooltip.hover title="cancel editing"></i>
        </div>

        <div v-if="editing == comment.id">
        </div>

   showComment(id) {
       this.editing = id
   },

关于javascript - 在 v-for 内的 vue js 中编辑状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58937380/

相关文章:

javascript - 使用 Vuex 时 $router.push() not a function 错误

methods - vue.js 是否更新依赖于方法的计算属性?

javascript - 从页面隐藏 div

php - 为什么这个 AJAX 请求在应该进行多次调用时会花费这么长时间?

javascript - 如何使用我的 spotify api token ?

javascript - 导入单文件组件 VueJS

javascript - 由于 webpack import outport,Vuejs eventbus 触发多次?

javascript - Angularjs - 从 ng-repeated 指令访问共享服务数据

javascript - 为什么 watcher 在响应 vue.js 中的数据变化时比计算更好地执行异步或昂贵的操作?

javascript - 如何让 mousemove 事件在 vue 组件内正常运行?