老师看下为啥我的id绑定不成功

老师看下为啥我的id绑定不成功

相关截图:

https://img1.sycdn.imooc.com//climg/618cfa5409fe614018320905.jpg


https://img1.sycdn.imooc.com//climg/618cfaa309e3df4e20351096.jpg

相关代码:Content.vue

<template>
  <div class="content">
    <div class="category">
      <div
        :class="{
          category__item: true,
          'category__item--active': currentTab === item.tab,
        }"
        v-for="item in categories"
        :key="item.name"
        @click="() => handleCategoryClick(item.tab)"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="product">
      <div class="product__item" v-for="item in contentList" :key="item._id">
        <img
          class="product__item__img"
          src="http://www.dell-lee.com/imgs/vue3/near.png  "
          alt=""
        />
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ item.name }}</h4>
          <p class="product__item__sales">月售 {{ item.sales }} 件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{ item.price }}
            <span class="product__item__orign">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span class="product__number__minus">-</span>
          0
          <span class="product__number__plus">+</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import { useRoute } from "vue-router";
import { get } from "../../utils/request";
export default {
  name: "Content",
  setup() {
    const categories = [
      {
        name: "全部商品",
        tab: "all",
      },
      {
        name: "秒杀",
        tab: "seckill",
      },
      {
        name: "新鲜水果",
        tab: "fruit",
      },
    ];
    const data = reactive({
      currentTab: categories[0].tab,
      contentList: [],
    });

    const route = useRoute();
    console.log(route.params.id)
    const getContentData = async (tab) => {
      const result = await get(`/api/shop/${route.params.id}/products`, {
        tab,
      });
      if (result?.errno === 0 && result?.data?.length) {
        data.contentList = result.data;
      }
    };
    const handleCategoryClick = (tab) => {
      getContentData(tab);
      data.currentTab = tab;
    };
    getContentData("all");
    const { contentList, currentTab } = toRefs(data);
    return { categories, currentTab, contentList, handleCategoryClick };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.content {
  position: absolute;
  display: flex;
  left: 0;
  right: 0;
  top: 1.5rem;
  bottom: 0.5rem;
}
.category {
  overflow-y: scroll;
  height: 100%;
  width: 0.76rem;
  background: $search-bgclor;
  &__item {
    line-height: 0.4rem;
    text-align: center;
    font-size: 0.14rem;
    color: $content-fontcolor;
    &--active {
      background-color: #fff;
    }
  }
}
.product {
  flex: 1;
  overflow-y: scroll;
  &__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgclor;
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: 0.68rem;
      height: 0.68rem;
      margin-right: 16px;
    }
    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontcolor;
      @include ellipsis;
    }
    &__sales {
      margin: 0.06rem 0;
      line-height: 0.16rem;
      font-size: 0.12rem;
      color: $content-fontcolor;
    }
    &__price {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $highlight-fontColor;
    }
    &__yen {
      font-size: 0.12rem;
    }
    &__orign {
      margin-left: 0.06rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
    .product__number {
      position: absolute;
      right: 0;
      bottom: 0.12rem;
      &__minus,
      &__plus {
        display: inline-block;
        width: 0.18rem;
        height: 0.18rem;
        line-height: 0.16rem;
        border-radius: 50%;
        font-size: 0.2rem;
        text-align: center;
      }
      &__minus {
        border: 0.01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: 0.05 rem;
      }
      &__plus {
        width: 0.2rem;
        height: 0.2rem;
        background: $btn-bgColor;
        color: $bgColor;
        margin-left: 0.05rem;
      }
    }
  }
}
</style>


正在回答

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

1回答

同学你好,id已经绑定成功了:

https://img1.sycdn.imooc.com//climg/618dcb2409c81d7611360412.jpg

https://img1.sycdn.imooc.com//climg/618dcb360965be2414460480.jpg

只是我们的接口是“模拟的(假的)”,它无法根据id的不同,返回不同的数据(不论id是什么,它都只能返回一样的数据),所以从页面效果上看,好像id没有绑定成功。

祝学习愉快!

  • 我不是胖球球 提问者 #1
    不管点击哪个tab,获取的id都是1,也是正确的吗?
    2021-11-12 10:18:49
  • 同学你好,是正确的。id是当前商品的唯一标识,点击不同的tab,都是请求同一商店下的不同数据,所以id是一样的。点击左侧tab栏,改变的是url地址上的tab参数:

    https://img1.sycdn.imooc.com//climg/618ddba80942ceac17380236.jpg

    https://img1.sycdn.imooc.com//climg/618ddbb609ee269717330320.jpg

    即根据tab参数的不同,请求同一商店的相应数据。

    点击如下位置,才会展示不同的商店,此时id会变化:

    https://img1.sycdn.imooc.com//climg/618ddbea095e6ddd04270253.jpg

    https://img1.sycdn.imooc.com//climg/618ddb5b090bea3618200631.jpg

    祝学习愉快!

    2021-11-12 11:14:27
  • 明白了,谢谢老师!!!

    2021-11-12 12:36:38
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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