ajax报错

ajax报错

相关代码:

 ERROR  Failed to compile with 1 error                                      8:21:27 ├F10: PM┤

 error  in ./src/utils/ajax.js

Module not found: Error: Can't resolve 'axios' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\utils'

ERROR in ./src/utils/ajax.js 1:0-26
Module not found: Error: Can't resolve 'axios' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\utils'
 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40[0].rules[0].use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/home/BannerView.vue?vue&type=script&lang=js& 11:0-36 22:6-14
 @ ./src/components/home/BannerView.vue?vue&type=script&lang=js& 1:0-222 1:238-241 1:243-462 1:243-462
 @ ./src/components/home/BannerView.vue 2:0-62 3:0-57 3:0-57 10:2-8
 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40[0].rules[0].use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/views/HomeView.vue?vue&type=script&lang=js& 15:0-50 23:12-18
 @ ./src/views/HomeView.vue?vue&type=script&lang=js& 1:0-214 1:230-233 1:235-446 1:235-446
 @ ./src/views/HomeView.vue 2:0-60 3:0-55 3:0-55 10:2-8
 @ ./src/router/index.js 6:0-45 11:13-21
 @ ./src/main.js 11:0-30 25:10-16

webpack compiled with 1 error

相关代码:

<template>
<!-- 首页轮播图 -->
  <div class="home-banner-box">
    <van-swipe class="my-swipe" :autoplay="3000" touchable=true indicator-color="white">
      <van-swipe-item v-for="item in bannerList" :key="item.id">
        <img :src="item.img" alt="" />
      </van-swipe-item>
    </van-swipe>
  </div>
</template>
<script>
import { ajax } from '@/utils/ajax'
import { SystemApis } from '@/utils/apis'
export default {
  data () {
    return {
      bannerList: []
    }
  },
  methods: {
    // 获取轮播图的数据
    getDatelist () {
      ajax.get(SystemApis.sliderListUrl).then(res => {
        console.log('res:', res)
      })
    }
  },
  created () {
    // TODO 从接口获取数据
    this.getDatelist()
    this.bannerList = [
      { id: 1, img: '/static/home/banner/banner1.jpg' },
      { id: 2, img: '/static/home/banner/banner2.jpg' },
      { id: 3, img: '/static/home/banner/banner3.jpg' }
    ]
  }
}
</script>
<style lang="less">
.home-banner-box {
  img {
    width: 100%;
    height: auto;
  }
}
</style>

相关代码:

import axios from 'axios'

export const ajax = axios.create({
  headers: {
    source: 'h5',
    icode: 'JFC680156494E2544',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  withCredentials: true
})


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕燕燕 2022-04-09 10:28:13

同学,你好!错误信息提示不能解析axios,同学确认一下当前项目中是否安装了axios,安装命令:npm install axios --save,安装完成后在package.json下可以看到axios依赖

https://img1.sycdn.imooc.com//climg/6250ef250935c4be05340302.jpg

祝:学习愉快!

  • 提问者 易动互推 #1
    邮件发了代码
    2022-04-09 10:31:08
  • 好帮手慕燕燕 回复 提问者 易动互推 #2

    同学,你好!同学使用是vue3,课程中使用的是vue2,vue3与vue2在语法上有很多不兼容,使用vue3无法运行本课程的代码,需要将vue3卸载,重新安装vue2版本。

    祝:学习愉快!

    2022-04-09 10:54:49
易动互推 提问者 2022-04-08 20:33:02

一番折腾后,报错更多了

 ERROR  Failed to compile with 14 errors                                    8:32:09 ├F10: PM┤


 error  in ./src/App.vue


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src' 


 error  in ./src/components/common/Footer.vue


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\components\common'


 error  in ./src/components/home/BannerView.vue


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\components\home'


 error  in ./src/views/AboutView.vue


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\views'


 error  in ./src/router/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\router'


 error  in ./src/store/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\store'


 error  in ./node_modules/vant/es/dialog/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\dialog'


 error  in ./node_modules/vant/es/image-preview/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\image-preview'


 error  in ./node_modules/vant/es/locale/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\locale'


 error  in ./node_modules/vant/es/notify/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\notify'


 error  in ./node_modules/vant/es/sku/Sku.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\sku'


 error  in ./node_modules/vant/es/toast/index.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\toast'


 error  in ./node_modules/vant/es/utils/create/component.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\utils\create'


 error  in ./node_modules/vant/es/utils/functional.js


Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\node_modules\vant\es\utils'


ERROR in ./src/App.vue 22:14-28

Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src'

 @ ./src/main.js 10:0-28 28:13-16


ERROR in ./src/components/common/Footer.vue 22:14-28

Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\components\common'

 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40[0].rules[0].use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/views/HomeView.vue?vue&type=script&lang=js& 18:0-52 29:16-26

 @ ./src/views/HomeView.vue?vue&type=script&lang=js& 1:0-214 1:230-233 1:235-446 1:235-446   

 @ ./src/views/HomeView.vue 2:0-60 3:0-55 3:0-55 10:2-8

 @ ./src/router/index.js 6:0-45 11:13-21

 @ ./src/main.js 11:0-30 25:10-16


ERROR in ./src/components/common/ListSight.vue 23:14-28

Module not found: Error: Can't resolve 'vue' in 'C:\Users\hejinghua\Desktop\trip-mobile\src\components\common'

 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40[0].rules[0].use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/home/FineView.vue?vue&type=script&lang=js& 20:0-54 23:15-24

 @ ./src/components/home/FineView.vue?vue&type=script&lang=js& 1:0-220 1:236-239 1:241-458 1:241-458

 @ ./src/components/home/FineView.vue 2:0-60 3:0-55 3:0-55 10:2-8

 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40[0].rules[0].use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/views/HomeView.vue?vue&type=script&lang=js& 17:0-46 27:10-14

 @ ./src/views/HomeView.vue?vue&type=script&lang=js& 1:0-214 1:230-233 1:235-446 1:235-446   

 @ ./src/views/HomeView.vue 2:0-60 3:0-55 3:0-55 10:2-8

 @ ./src/router/index.js 6:0-45 11:13-21

 @ ./src/main.js 11:0-30 25:10-16



  • 同学,你好!不确定同学具体是做了什么操作,如果安装axios之后还是有问题,可将node_modules整个目录删掉,然后终端运行npm i命令安装所有依赖,建议安装cnpm,cnpm i 从国内镜像下载依赖比较快

    https://img1.sycdn.imooc.com//climg/6250f07809e5f9d909020686.jpg

    祝:学习愉快!

    2022-04-09 10:36:38
  • 提问者 易动互推 #2
    我昨晚有百度之后删掉重装,但是一样20个报错,邮件里有写详细操作
    2022-04-09 10:38:01
  • 好帮手慕燕燕 回复 提问者 易动互推 #3

    同学,你好!老师这边运行了同学提供的代码,是由vue版本引起的,同学使用的是vue3,课程中使用的是vue2,vue3与vue2在语法上有很多不兼容,使用vue3无法运行本课程的代码,需要将vue3卸载,重新安装vue2版本。

    祝:学习愉快!

    2022-04-09 10:56:06
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师