图书渲染练习

先来看效果

 

 目标 通过vuex发送请求  渲染页面  代码:

store/index.js


import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
 
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    bookList: []
  },
  mutations: {
    // 获取图书列表
    getBooks (state, newList) {
      state.bookList = newList
    },
    delBook (state, index) {
      state.bookList.splice(index, 1)
    }
  },
  actions: {
    async ajax (state) {
      try {
        const { data } = await axios({
          url: 'https://www.fastmock.site/mock/37d3b9f13a48d528a9339fbed1b81bd5/book/api/books'
        })
        this.commit('getBooks', data.data)
      } catch (err) {
        console.log(err)
      }
    }
  },
  modules: {
  }
})

 shopCar 组件


<template>
  <div class="shop">
      <div class="shop-item" v-for="(obj,index) in $store.state.bookList" :key="index">
        <i class="el-icon-circle-close" @click="del(index)"></i>
        <img :src="obj.img" alt="">
        <p>{{obj.name}}</p>
        <span>{{obj.price}}</span>
      </div>
  </div>
</template>
 
<script>
export default {
  created () {
    this.$store.dispatch('ajax')
  },
  methods: {
    del (ind) {
      this.$store.commit('delBook', ind)
    }
  }
}
</script>
 
<style lang="less" scoped>
    div{
        box-sizing: border-box;
    }
    .shop{
        width: 900px;
        margin: 0 auto;
        margin-top: 50px;
        // background-color: #ccc;
        .shop-item{
            position: relative;
            float: left;
            width: 25%;
            height: 280px;
            // background-color: red;
            padding: 20px 27px;
            text-align: center;
            i{
                position: absolute;
                display: none;
                top: 5px;
                right: 2px;
                font-size:20px;
                color:skyblue
            }
            img{
                width:100%;
                height: 75%;
            }
            p{
                padding: 0 !important;
                margin: 0;
            }
            span{
                // margin-left: 60px;
                text-align: center;
            }
        }
        .shop-item:hover {
            box-shadow: 0 8px 8px rgba(10,16,20,.24),0 0 8px rgba(10,16,20,.12);
        }
        .shop-item:hover i{
            cursor: pointer;
            display: block;
        }
    }
</style>

app.vue

<template>
  <div>
    <ShopCar></ShopCar>
 
  </div>
</template>
 
<script>
import ShopCar from './views/02-购物车渲染.vue'
export default {
  name: '',
  props: {},
  data () {
    return {}
  },
  methods: {},
  computed: {},
  watch: {},
  created () {
  },
  mounted () {},
  components: {
    ShopCar
  }
}
</script>
 
<style scoped lang='less'>
 
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值