老师,问一个问题
我改数据就乱套了,不知道哪里出错了
<template>
<div class="content">
<div class="category">
<div
v-for="item in categoryData"
:key="item.name"
@click="()=>handleSwitchCategory(item.tab)"
:class="{'category__item':true, 'category__item--active': currentTab===item.tab}"
>{{item.name}}</div>
</div>
<div class="goods">
<div
v-for="item in list"
:key="item._id"
class="goods__item"
>
<img :src="item.imgUrl" class="goods__item__img">
<div class="goods__item__content">
<div class="goods__item__title">{{item.name}}</div>
<div class="goods__item__sale">月售 {{item.sales}} 件</div>
<div class="goods__item__price">
<span class="goods__item__yen">¥</span>{{item.price}}
<span class="goods__item__gray">{{item.oldPrice}}</span>
</div>
</div>
<div class="goods__number">
<span class="goods__number__minus">-</span>
0
<span class="goods__number__plus">+</span>
</div>
</div>
</div>
</div>
</template>
<script>
import { reactive, toRefs, ref, watchEffect} from '@vue/reactivity'
import { get } from '../../utils/request'
import { useRoute } from 'vue-router'
const categoryData=[
{name:'全部商品',tab:'all'},
{name:'秒杀',tab:'seckill'},
{name:'新鲜水果',tab:'fruit'}
]
const useTabEffect=()=>{
const currentTab=ref(categoryData[0].tab)
const handleSwitchCategory=(tab)=>{
currentTab.value=tab
}
return { currentTab, handleSwitchCategory}
}
const useCurrentListEffect=(currentTab)=>{
const content=reactive({
list:[]
})
const route = useRoute()
const getGoodsData= async()=>{
const result = await get(`/api/shop/${route.params.id}/products`,{tab:currentTab.value})
if(result.errno===0 && result.data.length){
content.list=result.data
}
}
watchEffect(()=>{
getGoodsData()
})
const {list}=toRefs(content)
return {list}
}
export default {
name:'Content',
setup(){
const { currentTab, handleSwitchCategory} = useTabEffect()
const {list} = useCurrentListEffect(currentTab)
return { list, currentTab, categoryData, handleSwitchCategory }
}
}
</script>
<style lang="scss" scoped>
.content{
position: absolute;
display: flex;
left: 0;
right: 0;
top: 1.57rem;
bottom: .5rem;
width: 100%;
}
.category{
flex-shrink: 0;
width: .76rem;
height: 100%;
background: #F5F5F5;
&__item{
text-align: center;
line-height: .4rem;
font-size: .14rem;
color: #333;
&--active{
background: #FFF;
}
}
}
.goods{
width: 100%;
height: 100%;
background: #FFF;
&__item{
position: relative;
display: flex;
margin: .12rem .18rem 0 .16rem;
border-bottom: .01rem solid #F1F1F1;
&__img{
display: block;
width: .68rem;
height: .68rem;
margin: 0 .16rem .12rem 0;
}
&__title{
font-size: .14rem;
color: #333;
font-weight: 600;
margin-bottom: .06rem;
}
&__sale{
font-size: .12rem;
color: #333;
margin-bottom: .08rem;
}
&__price{
font-size: .14rem;
color: #E93B3B;
font-weight: 600;
margin-right: .06rem;
}
&__yen{
font-size: .1rem;
font-weight: 400;
}
&__gray{
font-size: .1rem;
color: #999;
text-decoration: line-through;
}
}
&__number{
position: absolute;
right: 0;
bottom: 0;
margin-bottom: .12rem;
font-size: .14rem;
color: #333;
&__minus, &__plus{
display: inline-block;
font-size: .2rem;
line-height: .16rem;
width: .2rem;
height: .2rem;
border-radius: 50%;
text-align: center;
}
&__minus{
border: .01rem solid #666;
color: #666;
margin-right: .11rem;
}
&__plus{
margin-left: .14rem;
color: #FFF;
background: #0091FF;
}
}
}
</style>
正在回答
同学你好,watchEffect需要从vue中引入,同学做如下更改试试:
修改后,老师测试,代码没有问题。
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星