Vue package network module steps

Vue package network module

first step

Create a network folder in scr
Insert picture description here

Second step

Create three files in the network folder. The
Insert picture description here
first folder config.js, write the following `

// 定义请求方式
export const Method = {
    
    
    GET:"get",
    POST:"post"
}

export const PATH = {
    
    
    // 首页接口
    shouye_list:"/home/multidata"
}

The second folder core.js, write the following in it

// 引入axios
import axios from "axios"
// 引入config文件
import {
    
    Method} from "./config";

// 通过配置对象返回一个axios实例对象
const instance = axios.create({
    
    
    // 路由地址(前面相同)
    baseURL:"http://123.207.32.32:8000/api/x6",
    // 请求超时时间
    timeout:10000
})

export function request(method,url,parasm){
    
    
    switch(method){
    
    
        case Method.GET:
            return GET(url,parasm);
        case Method.POST:
            return POST(ulr,parasm)
    }
}

function GET(url,parasm){
    
    
    return instance.get(url,parasm)
}

function POST(url,params){
    
    
    return instance.post(url,params)
}

Write the following in the third index.js folder

// 引入core文件
import {
    
    request} from "./core"
// 引入config文件
import {
    
    Method,PATH} from "./config"

const netClient = {
    
    
  // 首页路径
  shouye_list(params){
    
    
    return request(Method.GET,PATH.shouye_list,params)
  },
}

// 抛出netClient
export default netClient;

third step

After writing, it needs to be introduced in min.js

// 网络模块
import netClient from "./network/index"
// 网络模块
Vue.prototype.$netClient = netClient;

the fourth step

You can test it in home.vue

mounted(){
    
    
    this.$netClient.shouye_list().then(res=>{
    
    
      console.log(res.data.data)
    })
  }

Guess you like

Origin blog.csdn.net/zxlong020/article/details/108267852