簡體   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