WeChatアプレットは、Dov.jsを使用して非同期リクエストを実装します

Dov.jsを使用して非同期リクエストを実装する


  • Dov.jsアドレス:https://gitee.com/DocBug/dov-http-mini

1.Dov.jsを紹介します

  • ダウンロード:npm install dov-http-mini
  • dov.min.jsファイルを独自のプロジェクトにコピーします
  • 必要なページのjsファイルにインポートします

2.クイック使用

//  设置默认地址(和axios的使用方法一致)
dov.defaults.baseURL = 'http://www.baidu.com'   
//  get请求(请求地址为http://www.baidu.com/user)
dov.get('user').then(response => {
    
    
    console.log(response)
})

//  发送请求时可以添加参数
dov.get('http://www.baidu.com/user', {
    
    
//  参数
    data: {
    
    
        username: 'dov',
        password: 'asdkln211232345sa'
    }
}).then(response => {
    
    
    console.log(response)
})

dov api


dov({
    
    
  method: 'post',
  url: 'http://www.baidu.com/getUserInfo',
  data: {
    
    
    username: 'king',
    password: 'kingpassword'
  }
}).then(response => {
    
    
  console.log(response)
})

3.WeChatミニプログラムでのリクエスト方法

  • 取得する
  • 役職
  • プット
  • 削除、
  • オプション、
  • 頭、
  • 痕跡、
  • 接続する

4.インスタンスオブジェクトを作成します

let server1 = dov.create({
    
    
    baseURL: 'https://api.baidu.com'
})
let server2 = dov.create({
    
    
    baseURL: 'https://img.baidu.com'
})

server1.get('/getUserInfo').then(response => {
    
    
    console.log(response)
})

インターセプターを構成する

dov.interceptors.request.use(function (config) {
    
    
    config.data.token = wx.getStorageSync('token')
    // ...
    return config
})

おすすめ

転載: blog.csdn.net/weixin_40944062/article/details/105030305