简体   繁体   中英

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

I defined a vue-resource:

resources.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:

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

This ajax call makes a request to:

/team/3233/details

Problem:

But when I use .update or .save rather than .get , the request url is not as expect:

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.

How can I specifies the url parameters in such a .save or .update ajax call?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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