4-15 活动组件开发 报错,跑不出 接口API 提供的图片

来源:1-1 为什么要有这么一门课,看看就知道啦

weixin_慕工程3403633

2022-05-26

图片描述
是在用MySwiper组件时 用的那个 map 方法有问题吗?

下面是我Home.vue的源码:

<template>
  <div class="home">

    <div class="home-content">
      <!-- swiper -->
          <h1>234423</h1>
      <my-swiper :swiperImgs="swiperData.map(item => item.icon)" :height="swiperHeight"></my-swiper>      <!-- 520 activity -->
      <activity>
        <div class="activity-520">
            <img v-for="(item, index) in activityDatas" :key='index' :src="item.icon">
        </div>
      </activity>
    </div>
  </div>
</template>

<script>
import MySwiper from '@c/swiper/MySwiper.vue'
import Activity from '@c/currency/Activity.vue'

export default {
  components: {
    MySwiper,
    Activity
  },
  data: function () {
    return {
      swiperData: [],
      swiperHeight: '184px',
      activityDatas: []
    }
  },
  created: function () {
    this.initData();
  },
  methods: {
    initData: function () {
      // this.$http = axios;
      this.$http.get('/swiper')
        .then(data => {
          console.log(data)
          this.swiperData = data.list;
        }).catch(err => {
          console.log(err);
        });
      // 520 活动数据
      this.$http.get('/activitys')
        .then(data => {
          this.activityDatas = data.list;
        }).catch(err => {
          console.log(err);
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@css/style.scss';
  .home {
    width: 100%;
    height: 100%;
    background-color: $bgColor;
    font-size: 32px;
    &-content {
      height: 100%;

      .activity-520 {
        margin-top: px2rem(-8);
        border-top-left-radius: px2rem(8);
        border-top-right-radius: px2rem(8);

        img {
          display: inline-block;
          width: 33.33%
        }
      }
    }
  }
</style>

是后端API 返回的数据结构有问题吗?

图片描述

写回答

2回答

weixin_慕工程3403633

提问者

2022-05-27

是axios 提取数据搞错了, 还有组件里的 img 标签的 :src="item.icon", 之前是 :src="item”

0
0

Sunday

2022-05-26

你好

从你的报错中来看 swiperData 应该是一个 undefined 。 你需要查看下你的数据

0
2
Sunday
回复
weixin_慕工程3403633
搞定了就好。
2022-05-27
共2条回复

混合开发入门 Vue结合Android/iOS开发仿京东项目App

流行的混合开发实战入门,前端和原生开发同学不容错过

1108 学习 · 448 问题

查看课程