繁体   English   中英

Vue.js:vue-resource 使用 path 参数调用 resource.save()

[英]Vue.js: vue-resource calling resource.save() with path parameter

我定义了一个 vue 资源:

资源.js:

import Vue from 'vue'
import VueResource from 'vue-resource'

Vue.use(VueResource);

export const Team = Vue.resource('team{/id}{/action}');

因此,如果我对该资源调用 get 方法:

api = require('.api/resources')
api.Team.get({
    id: 3233,
    action: 'details',
}).then(function(response) {
    // ....
});

这个 ajax 调用请求:

/team/3233/details

问题:

但是当我使用.update.save而不是.get ,请求 url 并不像预期的那样:

api = require('.api/resources')
api.Team.update({
    id: 3233,
    action: 'details',
}).then(function(response) {
    // ....
});

我只向/team发送请求,并在请求正文中传输所有参数。

如何在这样的.save.update ajax 调用中指定 url 参数?

那么简单地在方法调用上添加第二个参数就可以了:

api = require('.api/resources')
api.Team.update({
    id: 3233,
    action: 'details',
}, {}).then(function(response) {
    // ....
});

当 vue-resource 在一个带有 body 的方法上调用 ajax 时,它接受一个或两个数据参数。

如果给出一个参数,则将其视为正文有效负载。

如果给出两个参数,第一个是路由参数,而第二个是正文有效负载。

所以,如果要指定路由参数,只需给一个空对象作为第二个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM