如何强制嵌套组件重新渲染

时间:2021-05-25 15:36:37

标签: vue.js

你好,这是我的两个对象

包含列的 TaskTable 对象

 <template>
        <ul class="tasks-columns" id="tasks-columns">

            <li class="col-sm-4 col-lg-2 task-column-wrapper list-unstyled" v-for="status in statuses" :id="status">
                <draggable :emptyInsertThreshold="10" @add="endDrag" ghost-class="ghost" group="tasks">
                        <task v-for="task in tasks[status]" :task="task" :key="task.id"></task>
                </draggable>
            </li>

        </ul>
</template>

<script>
import draggable from 'vuedraggable'

export default {
    data: function () {
        return {
            statuses: [],
            users: [],
            tasks: []
        }
    },
    components: {
        draggable
    },
    mounted() {
        this.loadStatuses();
        this.loadTasks();
        this.loadUsers();
    },
    methods: {
        loadStatuses: function () {
            axios.get('/api/tasks/get-statuses')
                .then((response) => {
                    this.statuses = response.data;
                })
                .catch(function (error) {
                    console.log(error);
                });
        },
        loadUsers: function () {
            axios.get('/api/users')
                .then((response) => {
                    this.users = response.data.data;
                })
                .catch(function (error) {
                    console.log(error);
                });
        },

        loadTasks: function () {
            axios.get('/api/tasks/tasks-for-table')
                .then((response) => {
                    this.tasks = response.data;
                })
                .catch(function (error) {
                    console.log(error);
                });
        },
        endDrag: function (event) {
            let column = event.to.parentElement.parentElement
            $.ajax({
                url: '/tasks/change-status',
                type: 'POST',
                data: {'task_id': event.item.id, 'status': column.id},
                success: function (data) {
                    if (data.success) {
                        Toastify({
                            text: "Task status has been successfully updated!",
                            duration: 3000,
                            destination: base_url + '/tasks' + "show/" +  event.item.id,
                            newWindow: true,
                            close: true,
                            className: 'custom-toast',
                            gravity: "top", // `top` or `bottom`
                            position: "center", // `left`, `center` or `right`
                            backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
                            stopOnFocus: true, // Prevents dismissing of toast on hover
                            onClick: function () {
                            } // Callback after click
                        }).showToast();

                    }
                }
            });
        }


    }
}
</script>

以及代表列的每个单元格的任务对象

<template>


                <div class="task-wrapper" style="padding: 10%" :id="task.id">
                    <div class="task" >
                        <section style="margin-bottom: 1rem;">
                            <label>
                                {{ task.title }}
                            </label>
                        </section>
                        <section style="margin-bottom: 0.5rem;" class="task-table-task-status">
                                    <span style="padding-right: 5px;" data-toggle="tooltip" data-placement="right"
                                          title="Status">
                                        {{ task.status }}
                                    </span>
                        </section>
                        <section>
                    <span style="padding-right: 5px;" data-toggle="tooltip" data-placement="right"
                          title="Estimate">{{ task.estimate }}</span>

                            <div v-if="task.id" class="float-right">
                                <a class="c-header-nav-link" data-toggle="tooltip" data-placement="bottom"
                                   title="Assignee">
                                    <div class="c-avatar" style="width: 25px; height: 25px">
                                        <img class="c-avatar-img" :src="'/uploads/avatars/'+task.user.avatar">
                                    </div>
                                </a>
                            </div>
                        </section>
                    </div>
                </div>

</template>

<script>

export default {
    props: ['task'],
    data: function () {
        return {}
    },
    components: {
        draggable
    },
    mounted() {
        this.loadTask();
    },
    methods: {
        loadTask: function () {

        },
        reloadTask: function () {
            console.log('test');
        }

    }
}
</script>

所以目前它们是可拖动的,并且列之间的拖动正在调用更新任务状态的端点,但是在 UI 中,任务状态是旧状态,我想强制更新已移动的任务对象。知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

前端部分没有任何更新(在 vue 中) 你需要做这样的事情:

endDrag: function (event) {
        let column = event.to.parentElement.parentElement
        $.ajax({
            url: '/tasks/change-status',
            type: 'POST',
            data: {'task_id': event.item.id, 'status': column.id},
            success: function (data) {
                if (data.success) {

                    const task = this.tasks.find(t => t.id === event.item.id);
                    task.status = column.id;

                    Toastify({
                        text: "Task status has been successfully updated!",
                        duration: 3000,
                        destination: base_url + '/tasks' + "show/" +  event.item.id,
                        newWindow: true,
                        close: true,
                        className: 'custom-toast',
                        gravity: "top", // `top` or `bottom`
                        position: "center", // `left`, `center` or `right`
                        backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
                        stopOnFocus: true, // Prevents dismissing of toast on hover
                        onClick: function () {
                        } // Callback after click
                    }).showToast();

                }
            }
        });
    }

于是找到要更新的任务:

const task = this.tasks.find(t => t.id === event.item.id);

并根据您的状态更新它

task.status = column.id;