有没有一种方法可以动态更新我的组件而无需刷新页面?

时间:2019-04-22 19:54:23

标签: laravel vue.js vuejs2

我有一个vue组件来构建卡,并且在该组件内还有另一个组件,我使用道具将数据传递给该组件,我目前使用Swal作为弹出窗口将新项目添加到数据库中,但是完成添加后我必须刷新页面才能看到数据。我想使用vue的全部原因是不必刷新页面即可查看更新的数据,而且我无法弄清楚。

这是我的Projects.vue

   import ProjectItem from './Projects/ProjectItem.vue';
    export default {
        name: "Projects",
        components: {
            ProjectItem
        },
    data() {
            return {
                    projects: []
                }
        },
        methods: {
            getProjects () {
                axios.get('/api/projects').then((res) => {this.projects = res.data});
            },
            addProject() {
                Swal.queue([{
                title: 'Add a New Project?',
                html:
                    '<label for="name" style="color: #000;font-weight: 700">Project Name<input id="name" class="swal2-input"></label>' +
                    '<label for="description" style="color: #000;font-weight: 700">Project Description<textarea id="description" rows="5" cols="15" class="swal2-input"></textarea></label>',
                showCancelButton: true,
                confirmButtonText: 'Create Project',
                showLoaderOnConfirm: true,
                preConfirm: (result) => {
                    return new Promise(function(resolve, reject) {
                        if (result) {
                            let name = $('#name').val();
                            let desc = $('#description').val();
                            axios.post('/api/projects', {title:name,description:desc})
                            .then(function(response){
                                Swal.insertQueueStep({
                                type: 'success',
                                title: 'Your project has been created!'
                                })
                                resolve();
                            })
                            .catch(function(error){
                                Swal.insertQueueStep({
                                type: 'error',
                                title: 'Something went wrong.'
                                })
                                console.log(error);
                                reject();
                            })
                        }
                    });
                }
                }])
            }
        },
        mounted () {
            this.getProjects();
        }

我将其绑定到我的Project.vue模板中的ProjectItem:

            <div class="table-responsive border-top">
                <table class="table card-table table-striped table-vcenter text-nowrap">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Project Name</th>
                            <th>Team</th>
                            <th>Date</th>
                            <th>Preview</th>
                        </tr>
                    </thead>
                        <project-item v-bind:projects="projects" />
                </table>

这是我的ProjectItem.vue:

<template>
    <tbody>
        <tr v-for="project in projects" :key="project.id">
            <td>{{ project.id }}</td>
            <td>{{ project.title }}</td>
            <td><div class="avatar-list avatar-list-stacked">
                    {{ project.description }}
                </div>
            </td>
            <td class="text-nowrap">{{ project.updated_at }}</td>
            <td class="w-1"><a href="#" class="icon"><i class="fa fa-eye"></i></a></td>
        </tr>
    </tbody>    
</template>
<script>
export default {
    name: "ProjectItem",
    props: ["projects"],
}
</script>

1 个答案:

答案 0 :(得分:1)

您必须将最近添加的项目插入产品数组。

如果您能够更改后端代码,则可以更改响应以包括项目。

this.projects.push(response.project);
相关问题