0
想要將參數(如http GET/POST參數)發送到vue模板。 我們如何做到這一點,並在(webpack或vue)vue,我們可以發送vue模板的請求參數
想要將參數(如http GET/POST參數)發送到vue模板。 我們如何做到這一點,並在(webpack或vue)vue,我們可以發送vue模板的請求參數
首先,它取決於您的庫,我使用vue-resource
,因爲我覺得易於使用和語義上與Vue兼容。
如果您想在GET中發送URL參數,它很簡單。
// Trashs a task, won't delete from db.
TrashTask: function (taskIndex, taskID, category) {
this.$http.delete('/task/' + taskID).then(response => response.json()).then(result => {
this.tasks.splice(taskIndex, 1);
this.notify("Task deleted");
this.UpdateCategoryCount(category, "-", 1);
}).catch(err => {
console.log(err);
this.notify("Unable to trash Tash");
});
},
POST/PUT參數雖然有點不同。
首先,定義你的數據,在這裏,我們發送的是this.task
。在$http.put
中,第二個參數是您傳遞的數據,第三個參數是{emulateJSON: true}
,這樣,JSON就會轉換爲您的常規參數,如name=sh&age=25
。
如果您想了解更多:https://github.com/thewhitetulip/intro-to-vuejs/blob/master/5_interacting_with_backend.md
完整代碼:https://github.com/thewhitetulip/Tasks-vue/blob/master/public/static/js/app.js#L227
AddTask: function (item) {
this.$http.put('/task/', this.task, {
emulateJSON: true
}).then(response => response).then(result => {
if (this.task.ishidden == false) {
this.tasks.push(this.task);
}
this.UpdateCategoryCount(this.task.category, "+", 1);
this.task = {
title: '',
content: '',
category: '',
priority: '',
comments: [],
showComment: false
}
}).catch(err => {
console.log(err);
this.notify("Unable to add Task");
});
$('#addNoteModal').modal('hide');
},
請澄清你的問題 - 你可以發送HTTP請求到組件。 –