首页 > 解决方案 > 如何使用 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调用这些函数..

标签: vue.jsaxios

解决方案


您可以将 axios 作为 Vue 属性注入: Vue.prototype.$http = require('axios'); 然后您可以像这样使用它:this.$http.get(...) 或者您可以签出这个存储库


推荐阅读