当前位置:首页 » 《随便一记》 » 正文

案例: 购物车渲染_aichixiangcaia的博客

12 人参与  2022年03月02日 10:18  分类 : 《随便一记》  评论

点击全文阅读


 步骤: 1. 搭一下结构,

            2. 下载axios包

             3. 映入axios包

              4. 在created() {} 发送请求获取数据

                5.  将数据利用v-for循环到页面中(在data里面声明一个空数组,来装载请求回来的数据)

                6. 调整样式

                7. 根据索引删除当前的li

<template>
  <div>
    <h2>我的购物车</h2>
    <ul>
      <li v-for="(item,index) in list" :key="index">
        <img :src="item.img" alt="">
        <p>{{item.name}}</p>
        <p>{{item.price}}</p>
        <div class="off" @click="btn(index)">×</div>
      </li>
    </ul>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: '',
  props: {},
  data () {
    return {
      list: []
    }
  },
  methods: {
    btn (index) {
      this.list.splice(index, 1)
    }
  },
  computed: {},
  watch: {},
  created () {
    axios({
      url: 'https://www.fastmock.site/mock/37d3b9f13a48d528a9339fbed1b81bd5/book/api/books'
    }).then(res => {
      console.log(res)
      this.list = res.data.data
    })
  },
  mounted () {},
  components: {}
}
</script>

<style scoped>
h2 {
  text-align: center;
}
li {
  position: relative;
  width: 190px;
  height: 240px;
  list-style: none;
  float: left;
  box-shadow: 3px;
}

img {
  width: 180px;
  height: 200px;
}
.off {
  position: absolute;
  right: 20px;
  top: 5px;
  width: 30px;
  height: 30px;
  background-color: #ccc;
  text-align: center;
}
.off:hover {
  background-color: orange;
}
</style>


点击全文阅读


本文链接:http://zhangshiyu.com/post/35616.html

数据  请求  数组  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1