老师,有关代码复用的问题

老师,有关代码复用的问题

https://img1.sycdn.imooc.com/climg/6606152e0918d09808161396.jpg

https://img1.sycdn.imooc.com/climg/66061538094d992926361418.jpg

相关代码:CartView.vue

<template>
<div class="wrapper">
<div class="title">我的全部购物车(2)</div>
<div class="product">
<div class="product__title">{{ productListOne.shopName }}</div>
<div class="product__content" :style="showCart ? ShowCartList : ''">
<template v-for="item in productListOne.productList" :key="item._id">
<div class="product__content__item" v-if="item.count > 0">
<img
class="product__content__item__img"
:src="item.imgUrl"
alt=""
/>
<div class="product__content__item__detail">
<h4 class="product__content__item__title">{{ item.name }}</h4>
<p class="product__content__item__price">
<span class="product__content__item__yen"
>&yen;{{ item.price }} * {{ item.count }}</span
>
<span class="product__content__item__origin"
>&yen;{{ (item.price * item.count).toFixed(2) }}</span
>
</p>
</div>
</div>
</template>
</div>
<div class="product__total" @click="handleHideBtnClick">
共计{{ total }}件/1.4kg
<span>箭头</span>
</div>
</div>
<div class="product">
<div class="product__title">{{ productListTwo.shopName }}</div>
<div class="product__content">
<template v-for="item in productListTwo.productList" :key="item._id">
<div class="product__content__item" v-if="item.count > 0">
<img
class="product__content__item__img"
:src="item.imgUrl"
alt=""
/>
<div class="product__content__item__detail">
<h4 class="product__content__item__title">{{ item.name }}</h4>
<p class="product__content__item__price">
<span class="product__content__item__yen"
>&yen;{{ item.price }} * {{ item.count }}</span
>
<span class="product__content__item__origin"
>&yen;{{ (item.price * item.count).toFixed(2) }}</span
>
</p>
</div>
</div>
</template>
</div>
</div>
</div>
<DockView :currentIndex="1" />
</template>

<script>
import DockView from "../../components/DockView";
import { useStore } from "vuex";
import { ref } from "vue";
import { useCommonCartEffect } from "../../effects/catEffects";

const ShowCartList = {
height: "1.08rem",
overflow: "hidden",
marginBottom: ".16rem",
};

export default {
name: "CartView",
components: { DockView },
setup() {
const store = useStore();

const shopIdOne = 1;
const shopIdTwo = 2;

const productListOne = store.state.cartList[shopIdOne];
const productListTwo = store.state.cartList[shopIdTwo];

const showCart = ref(true);
const handleHideBtnClick = () => {
showCart.value = !showCart.value;
};

const { calculations } = useCommonCartEffect(shopIdOne);
const { total } = calculations.value;

return {
productListOne,
productListTwo,
handleHideBtnClick,
showCart,
ShowCartList,
total,
};
},
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.wrapper {
position: absolute;
overflow-y: scroll;
top: 0;
left: 0;
bottom: 0.49rem;
right: 0;
background: #f5f5f5;
padding: 0 0.16rem;
}

.title {
height: 0.44rem;
background-color: #fff;
line-height: 0.44rem;
text-align: center;
font-size: 16px;
color: #333333;
margin: 0 -0.18rem 0.16rem -0.18rem;
}

.product {
overflow-y: scroll;
flex: 1;
padding: 0.12rem 0.16rem 0 0.16rem;
background: #ffffff;
border-radius: 0.04rem;
margin-bottom: 0.16rem;
&__title {
font-size: 0.16rem;
line-height: 0.2rem;
margin: 0 0 0.16rem 0;
}
&__content {
&__item {
position: relative;
display: flex;
margin: 0 0 0.16rem 0;
&__img {
width: 0.46rem;
height: 0.46rem;
margin-right: 0.16rem;
}
&__detail {
overflow: hidden;
flex: 1;
color: $content-fontcolor;
}
&__title {
margin: 0 0 0.06rem 0;
font-size: 14px;
color: #333333;
line-height: 0.2rem;
}
&__price {
display: flex;
margin: 0;
line-height: 0.2rem;
}
&__yen {
flex: 1;
font-size: 0.12rem;
color: #e93b3b;
}
&__origin {
line-height: 0.2rem;
font-size: 0.12rem;
color: #000000;
}
}
}
&__total {
line-height: 0.28rem;
background: #f5f5f5;
font-size: 0.14rem;
color: #999999;
margin-bottom: 0.16rem;
text-align: center;
}
}
</style>


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

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

1回答
好帮手慕久久 2024-03-29 10:07:17

同学你好,可以复用,简单给同学举个例子,同学自己实现一下:

https://img1.sycdn.imooc.com/climg/660621d709ceb0e814430687.jpg

https://img1.sycdn.imooc.com/climg/660621f609dd53d114430693.jpg

https://img1.sycdn.imooc.com/climg/66062227098c5c7a13790632.jpg

同学参考一下,独立抽离一下组件。

如果实现不出来,建议提交作业,让批复作业的老师,根据同学的完整代码,给同学提供实现思路或方案。

祝学习愉快!


  • 提问者 派大星爱学习 #1

    老师 修改代码之后{{ item.productList.length }}没有被渲染到页面 是什么原因

    https://img1.sycdn.imooc.com/climg/6606564c092afc8107361328.jpg

    https://img1.sycdn.imooc.com/climg/6606565e095e0fd723581476.jpg

    <template>
    <template v-for="item in cartListWithProducts" :key="item.shopId">
    <div class="product">
    <div class="product__content">
    <h4 class="product__content__title">
    {{ item.shopName }}
    </h4>
    <template v-for="innerItem in item.productList" :key="innerItem.name">
    <div class="product__item" v-if="innerItem.count > 0">
    <img class="product__item__img" :src="innerItem.imgUrl" alt="" />
    <div class="product__item__detail">
    <div class="product__item__title">{{ innerItem.name }}</div>
    <p class="product__item__price">
    <span class="product__item__yen"
    >&yen;{{ innerItem.price }} * {{ innerItem.count }}</span
    >
    <span class="product__item__origin"
    >&yen;{{
    (innerItem.price * innerItem.count).toFixed(2)
    }}</span
    >
    </p>
    </div>
    </div>
    </template>
    </div>
    
    <div class="product__foot">
    <div class="product__foot__price" @click="handleShowHideClick">
    共计{{ item.productList.length }}件/1.4kg
    <span class="product__foot__icon iconfont">&#xe609;</span>
    </div>
    </div>
    </div>
    </template>
    </template>
    
    <script>
    import { ref , watchEffect} from "vue";
    
    // 溢出隐藏功能
    const useNearHideEffect = () => {
    let showHide = ref(true);
    
    const handleShowHideClick = () => {
    showHide.value = !showHide.value;
    };
    
    return { showHide, handleShowHideClick };
    };
    
    const useCartListEffect = () => {
    const cartListWithProducts = ref([]);
    const cartList = JSON.parse(localStorage.cartList);
    
    const handleLocalList = () => {
    for (let i in cartList) {
    if (i <= 2) {
    cartList[i].shopId = parseInt(i);
    cartListWithProducts.value.push(cartList[i]);
    }
    }
    };
    
    watchEffect(() => {
    handleLocalList();
    });
    
    return { cartListWithProducts };
    };
    
    export default {
    name: "CartContent",
    setup() {
    const { showHide, handleShowHideClick } = useNearHideEffect();
    
    const { cartListWithProducts } = useCartListEffect();
    
    return { showHide, handleShowHideClick, cartListWithProducts };
    },
    };
    </script>
    
    <style lang="scss" scoped>
    @import "../../style/viriables.scss";
    .product {
    padding: 0.16rem;
    background: $bgColor;
    border-radius: 4px;
    margin-bottom: 0.16rem;
    .product__content {
    &__title {
    line-height: 0.22rem;
    margin: 0 0 0.16rem 0;
    font-size: 16px;
    color: $content-fontcolor;
    }
    .product__item {
    display: flex;
    margin-bottom: 0.16rem;
    &__img {
    width: 0.46rem;
    height: 0.46rem;
    margin-right: 0.16rem;
    }
    &__detail {
    overflow: hidden;
    flex: 1;
    color: $content-fontcolor;
    }
    &__title {
    font-size: 14px;
    color: $content-fontcolor;
    line-height: 0.2rem;
    margin: 0 0 0.06rem 0;
    }
    &__sales {
    margin: 0.06rem 0;
    font-size: 0.12rem;
    line-height: 0.16rem;
    }
    &__price {
    display: flex;
    justify-content: space-between;
    margin: 0;
    line-height: 0.2rem;
    font-size: 0.14rem;
    color: $hightlight-fontColor;
    }
    &__origin {
    color: $dark-fontColor;
    }
    }
    }
    &__foot {
    width: 3.07rem;
    background: $search-bgColor;
    text-align: center;
    line-height: 0.28rem;
    font-size: 14px;
    color: $bg-Order-Color;
    &__icon {
    display: inline-block;
    transform: rotate(-90deg);
    font-size: 0.23rem;
    vertical-align: top;
    color: $bg-Order-Color;
    line-height: 0.28rem;
    }
    }
    }
    </style>


    2024-03-29 13:49:42
  • 最好给老师一个完整的项目,否则这一部分,很难在我本地复现出同学的问题。而且即使同一个问题,不同的代码,原因也不同。可以给网盘链接。

    2024-03-29 14:26:01
  • 链接: https://pan.baidu.com/s/1FMsSAu6dl4iyYw2pu46zdA 提取码: 5nxu

    2024-03-29 15:38:15
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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