VUE 笔记 ilovecoding 223 Toast封装-普通方式的封装

1. 增加组件 Toast.vue

<template>
  <div class="toast" v-show="show">
    <div>{{message}}</div>
  </div>
</template>

<script>
export default {
  name: 'Toast',
  props: {
    message: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style scoped>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 8px 10px;

  color: #fff;
  background-color: rgba(0, 0, 0, .75);
}
</style>

2. Detail.vue 使用 Toast.vue

    <detail-bottom-bar @addCart="addToCart"/>
    <back-top @click="backClick" v-show="isShowBackTop"/>
    <toast :message="message" :show="show"/>
  </div>
  import Toast from 'components/common/toast/Toast'
      Scroll,
      GoodsList,
      Toast
    },
        themeTopYs: [],
        currentIndex: 0,
        message: '',
        show: false
      }
      addToCart() {
        // 1. 获取购物车需要展示的信息
        const product = {}
        product.image = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.realPrice
        product.iid = this.iid
        // 2. 将商品添加到购物车里
        this.addCart(product).then(res => {
          this.show = true;
          this.message = res;
          setTimeout(() => {
            this.show = false;
            this.message = '';
          }, 1500)
        })
      }
posted @ 2023-01-01 18:04  君子键  阅读(47)  评论(0)    收藏  举报