2 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個贊
方法是利用vue-resource組件提供的一系列api:
get(url, [data], [success], [options])
post(url, [data], [success], [options])
put(url, [data], [success], [options])
patch(url, [data], [success], [options])
delete(url, [data], [success], [options])
jsonp(url, [data], [success], [options])
具體舉例如下:
1、導(dǎo)入vue-resource
<script src="js/vue.js"></script>
<script src="js/vue-resource.js"></script>
2、基于全局Vue對象使用http
// 通過someUrl獲取后臺數(shù)據(jù),成功后執(zhí)行then的代碼
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
3、在一個Vue實(shí)例內(nèi)使用$http
// $http是在vue的局部范圍內(nèi)的實(shí)例
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
說明:
在發(fā)送請求后,使用then方法來處理響應(yīng)結(jié)果,then方法有兩個參數(shù),第一個參數(shù)是響應(yīng)成功時的回調(diào)函數(shù),第二個參數(shù)是響應(yīng)失敗時的回調(diào)函數(shù)。

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個贊
在 同級的export defaut {} 中 定義data方法 返回一個對象, 這個對象里面就是 頁面綁定的數(shù)據(jù)模型參數(shù) , 在methods 對象的 函數(shù)里面 可以使用 data.數(shù)據(jù)模型名 調(diào)用
如
1 2 3 4 5 6 7 8 9 10 11 12 13 | <input v-model="param" @input="showParam"></input> export defaut { data() { return { param: 1 // 定義模型數(shù)據(jù) 綁定在 上面的input上 } }, methods: { showParam() { // 當(dāng) 輸入框 輸入時候會執(zhí)行這個函數(shù) 控制臺打印出輸出的數(shù)字 console.log(this.param) } } } |
添加回答
舉報