美文网首页
vue-resource 资源

vue-resource 资源

作者: 伍源辉 | 来源:发表于2017-01-16 17:23 被阅读37次

    Resource

    可以在全局的 Vue.resource 或者 Vue 实例 this.$resource 中使用 resource 服务。

    函数

    • resource(url, [params], [actions], [options])

    默认行为

    get: {method: 'GET'},
    save: {method: 'POST'},
    query: {method: 'GET'},
    update: {method: 'PUT'},
    remove: {method: 'DELETE'},
    delete: {method: 'DELETE'}
    

    示例

    {
      var resource = this.$resource('someItem{/id}');
    
      // GET someItem/1
      resource.get({id: 1}).then((response) => {
        this.$set('item', response.json())
      });
    
      // POST someItem/1
      resource.save({id: 1}, {item: this.item}).then((response) => {
        // success callback
      }, (response) => {
        // error callback
      });
    
      // DELETE someItem/1
      resource.delete({id: 1}).then((response) => {
        // success callback
      }, (response) => {
        // error callback
      });
    }
    

    自定义行为

    {
      var customActions = {
        foo: {method: 'GET', url: 'someItem/foo{/id}'},
        bar: {method: 'POST', url: 'someItem/bar{/id}'}
      }
    
      var resource = this.$resource('someItem{/id}', {}, customActions);
    
      // GET someItem/foo/1
      resource.foo({id: 1}).then((response) => {
        this.$set('item', response.json())
      });
    
      // POST someItem/bar/1
      resource.bar({id: 1}, {item: this.item}).then((response) => {
        // success callback
      }, (response) => {
        // error callback
      });
    }
    

    相关文章

      网友评论

          本文标题:vue-resource 资源

          本文链接:https://www.haomeiwen.com/subject/worpbttx.html