文章詳情頁
javascript - vue-resource 如何二次封裝
瀏覽:63日期:2023-03-01 08:23:10
問題描述
如何吧vue-resource 再次封裝到一個js文件里面 如:
let Ajax = { Vue.http.get(url,data).then(// ...代碼return data )}
然后在別的地方直接調用 如:
save(){ this.Ajax.get(url,data);}
問題解答
回答1:// api.jsexport default { save (params = {}) { return Vue.http.get(url, { params }).then(res => {// some handlingreturn res.data }) },// ...}
然后在其它文件中引入使用即可
import api from ’./api’api.save({ // params...}).then(data => { // ...})
用 axios 吧,官方已不推薦使用 vue-resource 了,使用 axios 配合 vue-axios 使用
回答2:全局注冊個插件
https://vuefe.cn/v2/guide/plu...
export default { install: function() { Vue.prototype.$ajax = Ajax; }}
然后use該文件,接著就可以使用啦
標簽:
JavaScript
相關文章:
1. php - mysql 模糊搜索問題2. html - 爬蟲時出現“DNS lookup failed”,打開網頁卻沒問題,這是什么情況?3. javascript - 在 vue里面用import引入js文件,結果為undefined4. php工具箱配置第二個vhost主機時不生效,報錯You don’t have permission5. php - 微信開發驗證服務器有效性6. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?7. [python2]local variable referenced before assignment問題8. javascript - js setTimeout在雙重for循環中如何使用?9. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?10. javascript - 求幫助 , ATOM不顯示界面!!!!
排行榜
