如何使用vue-resource插件按需删除对象?

时间:2015-12-24 19:17:43

标签: vue.js

使用vue-resource插件,它有一个这样的例子:

"Data Source=(localdb)\\mssqllocaldb;Initial Catalog=testdb01;Integrated Security=SSPI;"

我有点困惑,这是在new Vue({ ready: function() { var resource = this.$resource('someItem{/id}'); // delete item resource.delete({id: 1}).then(function (response) { // handle success }, function (response) { // handle error }); } }) 属性下,因为它表明它会在组件加载后立即运行和删除。如何通过单击元素实际使用ready函数?例如,我有这个:

resource.delete

目前称之为:

<div @click="deleteReward(reward)" class="glyphicon glyphicon-trash pull-right"></div>

但我的理解是,我应该能够以某种方式调用 deleteReward(reward) { this.rewards.$remove(reward); this.$http.delete('api/reward/' + reward.id).then(function (response) { console.log('deleted ' + reward.name); }); }, ,而不必明确指定URL和方法。我错了这个假设吗?

我想要的是做resource.delete之类的事情并让它知道自动调用@click="reward.delete()"并让resource.delete接受奖励对象作为参数。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

new Vue({
  data: {
    resource: null
  },

  ready: function() {
    this.resource = this.$resource('api/reward{/id}')
  },

  methods: {
    deleteReward(reward) {
      this.resource.delete({id:reward.id}).then(function (response) {
        console.log('deleted ' + reward.name)
      })
    }
  }
})

然后:

<div @click="deleteReward(reward)"></div>