封装api

2020-09-21  本文已影响0人  媛猿YY

新建/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;
            });
        },
上一篇 下一篇

猜你喜欢

热点阅读