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)
})
}