javascript - vue-resource 如何二次封裝
淡淡烟草味
淡淡烟草味 2017-07-05 10:58:37
0
2
1060

如何吧vue-resource 再次封裝到一個js檔案裡面 如:

let Ajax = {
    Vue.http.get(url,data).then(
        // ...代码
        return data
    )
}

然後在別的地方直接呼叫 如:

save(){
    this.Ajax.get(url,data);
}
淡淡烟草味
淡淡烟草味

全部回覆(2)
大家讲道理
// api.js

export default {
    save (params = {}) {
      return Vue.http.get(url, { params }).then(res => {
        // some handling
        return res.data
      })
    },
    
    // ...
}

然後在其它文件中引入使用即可

import api from './api'

api.save({
  // params...
}).then(data => {
  // ...
})

axios 吧,官方已不建議使用 vue-resource 了,使用 axios 配合 vue-axios 使用

仅有的幸福

全域註冊個外掛

https://vuefe.cn/v2/guide/plu...

export default {
  install: function() {
    Vue.prototype.$ajax = Ajax;
  }
}

然後use該文件,接著就可以使用啦

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!