Coder Social home page Coder Social logo

uniapp-request's Introduction

uniapp request

介绍

本项目借鉴项目134355/min-request的网络请求代码。

本项目根据该项目的网络请求二次开发,分离接口,按需引入。在于解决uniapp项目网络请求问题,统一管理请求、拦截和接口。

在本项目最新的更新中更改了原有的参数传入方式,实现了自动添加参数功能。

使用

  1. 项目中加入utils/

    mv utils <your-project>
  2. 在main.js中引入

    import request from "./utils/request";
    Vue.use(request);
  3. 修改服务请求地址

    // utils/api.js
    
    mRequest.setConfig(config => {
       config.baseURL = "http://server-url";
       return config;
    });
    • 开发时此处可使用环境变量来切换环境
  4. 创建api文件夹

    mkdir api
  5. 在api/下创建接口文件

    以用户登录为例,文件名为user.js

    import request from "../utils/api";
    
    export function login(data) {
       return request.post({
          url: "/user/login",
          data: data
       })
    }
  6. 页面中请求

    <template>
    	...
    </template>
    
    <script>
    import { login } from "../../api/user";
    
    export default {
       methods: {
          login({
             phone_number: "123123123",
             "verify_code": "123123"
          }).then(response => {
             console.log(response.data);
          })
    	}
    }
    </script>

更多请求方式

  • url添加多个参数,如token

    import request from "../utils/api";
    
    export function delUser(token, uid, data) {
        return request.post({
           url: "/user/delete",
           data: data,
           params: {token, uid}
        })
    }
    ...
    deluser(
        "tokenbalabala", // token
        12, //uid
        {	// 这里只是为了演示强行加入data
           username: "kuari",
           password: "adminadmin"
        }
    )
    ...

    请求时候url将变为:http://xxx.com/user/delete?token=tokenbalabala&uid=12

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.