http方法调用报错

来源:7-5 flyio 集成

o杨飞o

2019-08-04

老师你好,我在调用get和post方法的时候报错了。报错信息都是一样的。

vendor.js:17 Uncaught TypeError: __webpack_require__(...) is not a function
    at Object.<anonymous> (vendor.js:17)
    at __webpack_require__ (manifest.js:59)
    at Object._ (main.js:32)
    at __webpack_require__ (manifest.js:59)
    at Object.<anonymous> (vendor.js:6922)
    at __webpack_require__ (manifest.js:59)
    at Object.<anonymous> (vendor.js:6899)
    at __webpack_require__ (manifest.js:59)
    at Object.<anonymous> (vendor.js:6892)
    at __webpack_require__ (manifest.js:59)

我的代码:
/src/utils/request.js

function createFly() {
  // mpvuePlatform 是一个全局变量,平台类型
  if (mpvuePlatform === 'wx') {
    const Fly = require('flyio/dist/npm/wx')
    return new Fly()
  } else {
    // 其他的平台暂不支持
    return null
  }
}

function handleError(err) {
  console.log(err)
}

export function get(url, params = {}) {
  const fly = createFly()
  if (fly) {
    return new Promise((resolve, reject) => {
      fly.get(url, params).then(response => {
        console.log(response)
        resolve(response)
      }).catch(err => {
        console.log(err)
        handleError(err)
        reject(err)
      })
    })
  }
}

export function post(url, params = {}) {
  const fly = createFly()
  if (fly) {
    return new Promise((resolve, reject) => {
      fly.post(url, params).then(response => {
        console.log(response)
        resolve(response)
      }).catch(err => {
        console.log(err)
        handleError(err)
        reject(err)
      })
    })
  }
}

调用

<template>
  <div>
    <div class="font-color">
      Hello World
    </div>
    <van-button type="primary" @click="jump">按钮</van-button>
  </div>
</template>

<script>
  import { post } from '../../utils/request'
  export default {
    methods: {
      jump(){
        post('http://www.baidu.com').then(res => {
          console.log(res.data)
        })
      }
    }
  }
</script>

<style scoped lang="stylus">
  .font-color
    color: red
    font-size 20px
    font-weight bold
</style>

组件版本

    "flyio": "^0.6.14",
    "mpvue": "^2.0.0",
    "mpvue-router-patch": "^2.0.0",
    "vant-weapp": "^0.5.17",
    "vuex": "^3.0.1"
写回答

2回答

SkySF

2020-03-05

先npm run build,再npm run dev 成功解决问题

0
0

Sam

2019-08-04

群里已有热心同学帮忙解答了,重启服务即可

0
3
qq_慕码人2412777
回复
Sam
我解决了 哭了
2020-02-20
共3条回复

零基础入门 实战mpvue2.0多端小程序框架

mpvue,一套代码搞定多个平台小程序,前端更易上手

819 学习 · 647 问题

查看课程