我正在使用Laravel并try 学习Vue.js.我有一个正常工作的删除请求,正在从数据库中删除该对象.问题是,成功删除后,它不会从DOM中删除.我正在使用$remove方法并将其传递给完整对象,因此我知道我遗漏了一些东西.

作为旁注,我有一个main.js作为入口点,一个PersonTable.vue作为组件.PersonTable.vue保存该模板的模板和脚本.

以下是我对拉威尔的看法:

<div id="app">
    <person-table list="{{ $persons }}">

    </person-table>
</div>

这是我的"个性".vue:

<template id="persons-template">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <h1>Persons List</h1>
                <table class="table table-hover table-striped">
                    <thead>
                    <tr>
                        <td>First Name</td>
                        <td>Last Name</td>
                        <td>Email</td>
                        <td>Gender</td>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="person in list">
                        <td>{{person.first_name }}</td>
                        <td>{{person.last_name }}</td>
                        <td>{{person.email }}</td>
                        <td>{{person.gender }}</td>
                        <td><span @click="deletePerson(person)">X</span>
                    </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</template>

<script>
export default {

    template: '#persons-template',

    props: ['list'],

    methods: {
        deletePerson: function(person) {
            this.$http.delete('/person/' + person.id).then(
                function(response) {
                    this.persons.$remove(person);
                }
            );
        }

    },

    created: function() {
        this.persons = JSON.parse(this.list);
    }

};
</script>

我的main.js个切入点:

var Vue = require('vue');

Vue.use(require('vue-resource'));

var Token = document.querySelector('meta[name="_token"]').getAttribute('content');

Vue.http.headers.common['X-CSRF-TOKEN'] = Token;

import PersonTable from './components/PersonTable.vue';

new Vue({


    el: '#app',

    components: { PersonTable },

})

推荐答案

我认为需要将this绑定到响应函数:

function(response) {
    this.persons.$remove(person);
}.bind(this)

这样,当你做了this.persons次,你仍然是指Vue组件

编辑:可以试试-

props:['personJson'],
data:function(){
  return {
    persons:[]
  }
},
ready:function(){
  this.persons = JSON.parse(this.personJson)
}

考虑到最初persons是一个字符串,Vue没有正确绑定react 性功能?

Vue.js相关问答推荐

无法从CDN使用Vue和PrimeVue呈现DataTable

我可以将Created()中内置的内容传递给Provide属性吗?

带 Bootstrap 的 Vue js,导航栏菜单不起作用

Vuetify 3 v-radio-group 将模型设置为 null

Vuetify右侧并排switch

Vue 3组合式API如何测试是否发出正确的事件

vue 不重新渲染数据更改

Vue 3 / Vuetify 3:如何为按钮设置全局默认 colored颜色

如何在 Nativescript 应用程序中调用 axios/api 调用

使用 jquery-chosen 插件更新 vuejs 模型值

如何使用带有自定义标签和类的`transition-group`

如何在map中添加新的标记 onclick?

在 vue-router 中带有路由的 Vuejs2 模态

webpack模块解析失败意外字符'@'

基本 vue.js 2 和 vue-resource http 获取变量赋值

异步api调用后如何使用vuex getter

如何在 VueJS 中下载本地存储的文件

Vuex Mutation 正在运行,但组件在 vue 开发工具中手动提交之前不会更新

vuejs 中缺少 webpack 配置

Vue.js 中的条件 依赖于props值?