08-Vue:组件name属性、购物车案例

1.组件name属性

可以用组件的name属性值,来注册组件名字
问题:组件名不是可以随便写的?
我们封装的组件–可以自己定义name属性组件名-让使用者有个统一的前缀风格
在这里插入图片描述
在这里插入图片描述

2.购物车

项目初始化

vue create shopcar
yarn add bootstrap
yarn add less less-loader@5.0.0 -D

清空不要的东西:
1.把项目拆分成几个组件,components下创建: MyHeader、MyFoot、MyGood、MyCount;
2.创建的组件引入到HomeView.vue上;
3.main.js中引入bootstrap库;

// 引入第三方包里的css文件
import ‘bootstrap/dist/css/bootstrap.css’

布局
头部自定义
头部的标题 颜色 背景色可以随便更改 props类型校验
步骤:
1.在MyHeader.vue中定义props;
2.在使用MyHeader.vue组件时,传入相应的值(color,backgroundColor)

请求数据
1.下载axios并全局配置;
2.HomeView.vue组件中请求数据;
3.把数据传递给MyGood组件 渲染 并传递给数量组件

商品选中
在这里插入图片描述
数量控制
点击 + - 按钮 直接修改数量的输入框修改数量

<template>
  <div class="my-counter">
    <button @click="obj.goods_count++" type="button" class="btn btn-light">
      +
    </button>
    <input type="number" v-model="obj.goods_count" class="form-control inp" />
    <button
      :disabled="obj.goods_count === 1"
      @click="obj.goods_count > 1 && obj.goods_count--"
      type="button"
      class="btn btn-light"
    >
      -
    </button>
  </div>
</template>

<script>
export default {
  props: {
    obj: Object,
  },
  watch: {
    obj: {
      deep: true,
      handler() {
        if (this.obj.goods_count < 1) {
          this.obj.goods_count = 1;
        }
      },
    },
  },
};
</script>

<style lang="less" scoped>
.my-counter {
  display: flex;
  .inp {
    width: 45px;
    text-align: center;
    margin: 0 10px;
  }
  .btn,
  .inp {
    transform: scale(0.9);
  }
}
</style>

全选反选
1.点击获取它的选中状态;
2.同步给上面每个小选框 --小选框的选中状态又在数组里 子传父;
3.把数组传给My-Foot

总数量

<template>
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <input
        type="checkbox"
        class="form-check-input"
        v-model="isAll"
        id="footercheckbox"
      />
      <label for="footercheckbox" class="custom-control-label">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计</span>
      <span class="price">¥{{ allPrice }}</span>
    </div>
    <!-- 结算 -->
    <button type="button" class="footer-btn btn btn-primary">
      {{ allCount }}
    </button>
  </div>
</template>

<script>
export default {
  props: {
    list: Array,
  },
  computed: {
    isAll: {
      set(val) {
        // 同步到每一个小框
        this.$emit("changeAll", val);
      },
      get() {
        return this.list.every((item) => item.goods_state === true);
      },
    },
    allCount() {
      // 总数量
      return this.list.reduce((sum, item) => {
        if (item.goods_state === true) {
          sum += item.goods_count;
        }
        return sum;
      }, 0);
    },
    allPrice() {
      // 总价格
      return this.list.reduce((sum, item) => {
        if (item.goods_state === true) {
          sum += item.goods_count * item.goods_price;
        }
        return sum;
      }, 0);
    },
  },
};
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background-color: #fff;
  .price {
    color: red;
    font-weight: bold;
    font-size: 14px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值