vue.js - 如何使用 vuejs 使 axios 的 API 请求可重用?
问题描述
如何使axios 的API 请求可通过我的应用程序重用,这样我就可以避免一遍又一遍地编写相同的代码..
//load the axios HTTP library
window.axios = require('axios');
window.Vue = require('vue');
const app = new Vue({
el: '#app',
mounted(){
console.log('hello!');
}
});
我正在考虑尝试这样的事情(要点),但我不知道如何将函数附加到Vue 实例并直接从 vue-components调用这些函数..
解决方案
您可以将 axios 作为 Vue 属性注入:
Vue.prototype.$http = require('axios');
然后您可以像这样使用它:this.$http.get(...)
或者您可以签出这个存储库
推荐阅读
- javascript - this.state 在 .map 内为 0
- java - java.lang.NoClassDefFoundError: com.google.android.gms.common.internal.zzbo
- c - 如何水平移动星形图案?
- python - TypeError:在 Python2.7 中创建空字典时,'dict' 对象不可调用
- c# - 在 owin 中间件中读取响应体
- javascript - 我无法从 spotify 的 API 获取图像
- php - 如何在 PHP 中打印彩色字符串
- python - 将数据框中的列转换为“类”?
- bash - Bash:卷曲多行,将输出写入文件
- php - 如何通过xml在日期和时间中使用动态数据进行发布请求