【发布时间】:2016-07-07 22:37:24
【问题描述】:
我正在使用 Laravel 并尝试学习 Vue.js。我有一个正常工作的删除请求并从数据库中删除对象。问题是成功删除后它没有从 DOM 中删除。我正在使用$remove 方法并将完整的对象传递给它,所以我知道我遗漏了一些东西。
作为旁注,我有一个main.js 作为入口点,PersonTable.vue 作为一个组件。 PersonTable.vue 保存该模板的模板和脚本。
这是我的 Laravel 视图:
<div id="app">
<person-table list="{{ $persons }}">
</person-table>
</div>
这是我的`PersonTable.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 },
})
【问题讨论】:
-
你在完成删除请求后更新 Vue.data 吗?
-
尝试处理你的 post 请求中的错误响应,看看你得到了什么,如果你得到一个错误,那么你删除数据的代码部分永远不会执行
-
@DanWhite 没有。至少我没有明确更新它。不知道该怎么做。
-
@YerkoPalma 我在
this.persons.$remove(person)之后添加了console.log(person),console显示了被点击的人的对象。 -
您应该删除组件中创建的方法,我认为这是导致问题的原因。为什么需要这条线?告诉我我是否正确,我会将其发布为答案...但我认为就是这样:/
标签: javascript laravel vue.js vue-resource