[英]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.