请问老师一个问题

请问老师一个问题

当我点击购物车图标的时候,如果购物车本身什么都没有那么就不会显示任何东西,但是点击事件是执行了,当我再去点击商品列表添加东西时会自动弹出购物车和遮罩,那这里逻辑是不是有问题?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小李 2023-08-04 16:54:11

同学你好,你理解的是对的。可以加个变量进行判断如变量为false时购物车则不可点击,或者购物车图标隐藏。当为true时才可以进行点击或显示即可。

祝学习愉快!

  • 提问者 慕仙7313728 #1

    老师,你好,麻烦帮我看下。这里该怎么改,自己实在找不明白了。这里传入的show到vuex中显示未定义value。

    <template>
        <div class="mask" v-if="show&&total>0" @click="showCarList"></div>
        <div class="foot">
            <div class="foot_shopCar">
                <span class="foot_shopCar_shopNum">{{ total }}</span>
                <img src="http://www.dell-lee.com/imgs/vue3/basket.png" alt="" class="foot_shopCar_img" @click="()=>showCarList(total)">
                <span class="foot_shopCar_text" v-if="total == 0">购物车是空的</span>
                <span class="foot_shopCar_money" v-else> <span class="foot_shopCar_money_icon">合计:¥</span>{{ price }}</span>
            </div>
            <div class="foot_settlement">
                <router-link :to="{name:'Home'}">
                    去结算
                </router-link>
            </div>
        </div>
        <div class="content" v-if="show&&total>0">
            <!-- v-if="show&&total>0" -->
            <div class="content_select">
                <span class="content_all">
                    <span class="iconfont content_all_allChecked"  :class="[allChecked ? blue : '', black]"
                        @click="() => setAllChecked(shopId)">&#xe669;</span>
                    <!-- <span class="iconfont content_all_allChecked"
                    v-if="allChecked" style='color:#0091FF'>&#xe669;</span>
                    <span class="iconfont content_all_allChecked"
                    v-if="!allChecked" style='color: #000'>&#xe669;</span> -->
                    全选
                </span>
                <span class="content_ClearShopCart" @click="() => { clearShopCar(shopId,show) }">清除购物车</span>
            </div>
            <template v-for="item of productList" :key="item._id">
                <!-- v-if="item.count > 0" v-show="show" -->
                <div class="content_dec" v-if="item.count > 0">
                    <span class="iconfont content_dec_checked" :class="[item.checked ? blue : '', black]"
                        @click.stop="() => changeChecked(shopId, item._id)">&#xe605;</span>
                    <!-- v-html="item.checked ? '&#xe603;':'&#xe669;'" -->
                    <!-- <span class="iconfont content_dec_checked" v-if="item.checked == true"
                        @click.stop="() => changeChecked(shopId, item._id)">&#xe605;</span>
                    <span class="iconfont content_dec_nochecked" v-if="item.checked == false"
                        @click.stop="() => changeChecked(shopId, item._id)">&#xe605;</span> -->
                    <img :src="item.imgUrl" class="content_dec_img" alt="">
                    <div class="content_dec_commodity">
                        <ul class="commodity_list">
                            <li class="commodity_list_name">{{ item.name }}</li>
                            <li class="commodity_list_price">¥{{ item.price}}</li>
                        </ul>
                        <div class="commodity_list_oldprice">¥{{ item.oldPrice }}</div>
                    </div>
                    <div class="content_dec_addsub">
                        <p class="addsub_minus" @click="() => { changeShopContent(shopId, item._id, item, -1)}">—</p>
                        <p class="addsub_num">{{ carList?.[shopId]?.productList[item._id]?.count || 0 }}</p>
                        <p class="addsub_add" @click="() => { changeShopContent(shopId, item._id, item, 1) }">+</p>
                    </div>
                </div>
            </template>
        </div>
    </template>
    
    <script>
    import { computed, ref, toRef, watchEffect } from 'vue';
    import { useStore } from 'vuex';
    import { useRoute } from 'vue-router';
    import { useShopCarEffect } from './changeShopContent';
    //购物车计算逻辑
    const useCarComputedDeffect = (shopId,show) => {
        const store = useStore();
        const carList = store.state.carList;
        const total = computed(() => {
            const productList = carList[shopId]?.productList
            let count = 0
            if (productList) {
                for (let i in productList) {
                    const product = productList[i];
                    count += product.count;
                }
            }
            return count
        })
        const price = computed(() => {
            const productList = carList[shopId].productList
            let count = 0
            if (productList) {
                for (let i in productList) {
                    const product = productList[i];
                    if (product.checked) {
                        count += (product.count * product.price)
                    }
                }
            }
            return count.toFixed(2)
        })
    
        const productList = computed(() => {
            const productList = carList[shopId]?.productList || []
            return productList
        })
    
        const changeChecked = (shopId, productId) => {
            store.commit('toChangeChecked', {
                shopId, productId
            })
        }
        const clearShopCar = (shopId) => {
            show.value=false
            console.log(show);
            store.commit('toClearShopCar', {
                shopId
            })
        }
        const setAllChecked = (shopId) => {
            store.commit('toSetAllChecked', {
                shopId
            })
        }
        const allChecked = computed(() => {
            const productList = carList[shopId].productList
            let result = true
            if (productList) {
                for (let i in productList) {
                    const product = productList[i];
                    if (product.count > 0 && !product.checked) {
                        result = false
                    }
                }
            }
            return result
        })
        return {
            total, price, productList, changeChecked,
            clearShopCar, allChecked, setAllChecked
        }
    }
    
    export default {
        name: 'Car',
        setup() {
            const blue = 'blue'
            const black = 'black'
            const show = ref(false)//show控制显示与否
            const route = useRoute();
            const shopId = route.params.id;
            //购物车显示隐藏
            const showCarList = (total) => {
                if(total<1){
                    console.log(show.value);
                    return
                }
                console.log(total);
                show.value = !show.value
                console.log(show.value);
            }
            const { total, price, productList,
                changeChecked, clearShopCar,
                allChecked, setAllChecked } = useCarComputedDeffect(shopId,show)
            const { changeShopContent, carList } = useShopCarEffect(show)//传入show
            return {
                total, price, shopId, productList, carList,
                changeShopContent, changeChecked, clearShopCar, allChecked,
                setAllChecked, showCarList, blue, black, show
            }
        }
    
    }
    </script>
    import { useStore } from 'vuex';
    import {  toRefs,ref } from 'vue';
    //购物车相关数据
    export const useShopCarEffect = (show) => {
        //传入show
        const store = useStore();
        const { carList } = toRefs(store.state)
        const changeShopContent = (shopId, productId, productData,num) => {
            store.commit('changeShopContent',{
                shopId, productId, productData,num,show
                //传入show
            })
        }
        return { carList ,changeShopContent}
    }
    import { ref } from 'vue';
    import { createStore } from 'vuex'
    
    export default createStore({
      state: {
        carList: {
          //数据解构:
          // shopId:{
          //   shopName,
          //   productList:{
          //     productId:{
          //       _id:'1',
          //       name:'潘茄',
          //       imgurl:'xxx',
          //       sales:10,
          //       price:33.5,
          //       oldPrice:40.0,
          //       count:1
          //     }
          //   }
          // }
        }
      },
      getters: {
      },
      mutations: {
        changeShopContent(state,payload) {
          const { shopId, productId, productData } = payload;
          let shopInfo = state.carList[shopId]
          if (!shopInfo) {
            shopInfo = { shopId: '', productList: {} }
          }
          let productInfo = shopInfo.productList[productId]
          if (!productInfo) {
            productInfo = productData
            productInfo.count = 0
          }
          productInfo.count = productInfo.count + payload.num;
          if (payload.num > 0) {
            productInfo.checked = true
          }
          if (productInfo.count >= 99) {
            productInfo.count = 99
          }
          if (productInfo.count <= 0) {
            productInfo.count = 0
          }
          shopInfo.productList[productId] = productInfo
          state.carList[shopId] = shopInfo
          //改变show的值
          let total=0
          for(let key in state.carList[shopId].productList){
            total+=state.carList[shopId].productList[key].count
          }
          if(total==0){
            //报错,提示没有定义value
            payload.show.value=false
          }
        },
        changeShopName(state, payload) {
          const { shopId, shopName } = payload
          const shopInfo = state.carList[shopId] || {
            shopId: '', productList: {}
          }
          shopInfo.shopName = shopName
          state.carList[shopId] = shopInfo
    
        },
        toChangeChecked(state, payload) {
          const { shopId, productId } = payload
          let product = state.carList[shopId].productList[productId]
          product.checked = !product.checked
        },
        toClearShopCar(state, payload) {
          const { shopId} = payload
          state.carList[shopId].productList = {}
        },
        toAllChecked(state, payload) {
          const { shopId, productId } = payload;
          let product = state.carList[shopId].productList[productId]
          product.checked = true
        },
        toSetAllChecked(state, payload) {
          const { shopId } = payload;
          const product = state.carList[shopId].productList
          if (product) {
            for (let key in product) {
              const products = product[key]
              products.checked = true
            }
          }
        }
      },
      actions: {
      },
      modules: {
      }
    })


    2023-08-06 15:45:00
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师