老师,你好,麻烦帮我看下。这里该怎么改,自己实在找不明白了。这里传入的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)"></span>
<!-- <span class="iconfont content_all_allChecked"
v-if="allChecked" style='color:#0091FF'></span>
<span class="iconfont content_all_allChecked"
v-if="!allChecked" style='color: #000'></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)"></span>
<!-- v-html="item.checked ? '':''" -->
<!-- <span class="iconfont content_dec_checked" v-if="item.checked == true"
@click.stop="() => changeChecked(shopId, item._id)"></span>
<span class="iconfont content_dec_nochecked" v-if="item.checked == false"
@click.stop="() => changeChecked(shopId, item._id)"></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: {
}
})
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星