封装api

作者: 媛猿YY | 来源:发表于2020-09-21 11:16 被阅读0次

    新建/mimall/vue.config.js

    module.exports = {
    devServer: {
            host: '0.0.0.0',
            // open: true,
            port: 8899,
            proxy: {
                '/api/sdk': {
                    target: 'http://teg-test.58dns.org:9095',
                    changeOrigin: true,
                    pathRewrite: {
                        '/api/sdk': ''
                    }
                },
                '/api/media': {
                    target: 'http://teg-test.58dns.org:8089',
                    changeOrigin: true,
                    pathRewrite: {
                        '/api/media': ''
                    }
                },
                
    
            }
        }
    }
    

    新建/mimall/src/api/index.js,封装接口

    import Axios from 'axios';
    // get
    export const products = (param) => Axios.get('/api/media/products', { params: param });
    // post
    export const getBugInfo = (param) => Axios.post('/api/sdk/inspection/getBugInfo',  param );
    
    

    在main.js中挂载

    import * as api from './api/index'
    Vue.prototype.$api = api;
    

    在文件中请求接口

    getProducts() {
                // 请求接口
                let obj = {
                    categoryId: '100012',
                    pageSize: 6,
                };
                this.$api.products(obj).then((res) => {
                    // console.log('res: ', res);
                    this.res = res;
                });
            },
    

    相关文章

      网友评论

          本文标题:封装api

          本文链接:https://www.haomeiwen.com/subject/tqteektx.html