简体   繁体   English

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

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

I defined a vue-resource:我定义了一个 vue 资源:

resources.js:资源.js:

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

Vue.use(VueResource);

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

So, then if I call the get method on that resource:因此,如果我对该资源调用 get 方法:

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

This ajax call makes a request to:这个 ajax 调用请求:

/team/3233/details

Problem:问题:

But when I use .update or .save rather than .get , the request url is not as expect:但是当我使用.update.save而不是.get ,请求 url 并不像预期的那样:

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

I only send a request to /team with all parameters transferred in the request body.我只向/team发送请求,并在请求正文中传输所有参数。

How can I specifies the url parameters in such a .save or .update ajax call?如何在这样的.save.update ajax 调用中指定 url 参数?

Well simply adding a second parameter on the method call is ok:那么简单地在方法调用上添加第二个参数就可以了:

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

When the vue-resource calling ajax on a method with body, it accepts either one or two data parameter.当 vue-resource 在一个带有 body 的方法上调用 ajax 时,它接受一个或两个数据参数。

If one single parameter is given, it was treated as the body payload.如果给出一个参数,则将其视为正文有效负载。

If two parameters are given, the first is the routing parameter, while the second one is the body payload.如果给出两个参数,第一个是路由参数,而第二个是正文有效负载。

So, if want to specify the routing parameter, just give an empty object as the second parameter.所以,如果要指定路由参数,只需给一个空对象作为第二个参数。

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

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