【问题标题】:Is there a way to update my component dynamically without refreshing the page?有没有办法在不刷新页面的情况下动态更新我的组件?
【发布时间】:2019-04-22 19:54:22
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: laravel vue.js vuejs2


    【解决方案1】:

    您必须将最近添加的项目插入到 products 数组中。

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

    this.projects.push(response.project);
    

    【讨论】:

    • 这是我从后端检索数据的方式:``` public function index(Project $project) { $projects = Project::latest('created_at')->limit(5)->得到();返回响应()->json($projects); } ```
    • @Riley 不只是将结果限制为 5,也许您可​​以对它们进行分页,这样您就可以抓取它们的组并增加页面以获得下一组。
    • @Riley 我相信您不需要再次调用来检索项目数据。您介意分享响应对象中的内容吗?如果此对象包含项目,您可以将其添加到数组中。如果没有,您可以更改您的 store 方法(假设您使用 Laravel 资源控制器)以在响应中返回新项目。
    • @ThiagoBarcala 这是我的响应数据对象:data: {title: "ddd", description: "ddd"} & 当我尝试 this.projects.push(response.data);我得到一个参考错误:未定义项目
    • @Riley 这是因为“this”在 JavaScript 中的绑定方式。添加var self = this作为函数addProject的第一行,然后使用self.projects.push(response.data)。但它可能会给您一些错误,因为您的项目对象似乎需要一个 ID,而该 ID 不在您的响应数据中,因此您可能需要更改后端以包含它。
    猜你喜欢
    • 2020-08-29
    • 2010-09-26
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多