先來說微信小程序原生的請求接口方式吧,如官網文檔,在頁面中請求直接調用
在這里感謝接的是提供的免費接口https://blog.csdn.net/c__chao/article/details/78573737
wx.request({ url: 'https://api.apiopen.top/searchAuthors', //僅為示例,並非真實的接口地址 data: { name: '李白' }, header: { 'content-type': 'application/json' // 默認值 }, success (res) { console.log(res.data.result) } })
好了,不如正題說一下Flyio,依葫蘆畫瓢寫了一下
1.首先安裝flyio
npm install flyio --save
2.引入flyio。新建一個js文件,request.js
import Vue from 'vue'
const Fly = require("flyio/dist/npm/wx")
const fly = new Fly
fly.config.baseURL = 'https://api.apiopen.top/'
Vue.prototype.$http = fly
export default fly
還可以配置你的攔截器、響應時間等。。
3.頁面中請求接口,調用就可以啦
import request from '@/utils/request'
getRequest(){ request.get('recommendPoetry').then((d)=>{ console.log(this.article = d.data.result) }).catch(error=>{ console.log(error) }) }
好啦,運行一下,完美

