龙空技术网

vue3详情页基础数据渲染

tianlongbabu 40

前言:

目前兄弟们对“vue商品详情”可能比较讲究,小伙伴们都想要剖析一些“vue商品详情”的相关文章。那么小编也在网摘上收集了一些有关“vue商品详情””的相关知识,希望咱们能喜欢,姐妹们快快来了解一下吧!

1.封装接口apis/detail.js

import request from '@/utils/http'

export const getDetail = (id) => {

return request({

url: '/goods',

params: {

id

}

})

}

2.相关组件引入接口

views/Detail/index.vue

import { getDetail } from '@/apis/detail'

import { useRoute } from 'vue-router'

const goods = ref({})

const route = useRoute()

const getGoods = async () => {

const res = await getDetail(route.params.id)

goods.value = res.result

}

onMounted(() => getGoods())

3.相关组件进行渲染

--面包屑导航

<div class="container" v-if="goods.details">

<div class="bread-container">

<el-breadcrumb separator=">">

<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>

<!--

错误原因:goods一开始{} {}.categories -> undefined -> undefined[1]

1. 可选链的语法?.

如——goods.categories?.[1].id

2. v-if手动控制渲染时机 保证只有数据存在才渲染

如——v-if="goods.details"

-->

<el-breadcrumb-item :to="{ path: `/category/${goods.categories[1].id}` }">{{ goods.categories[1].name }}

</el-breadcrumb-item>

<el-breadcrumb-item :to="{ path: `/category/sub/${goods.categories[0].id}` }">{{

goods.categories[0].name

}}

</el-breadcrumb-item>

<el-breadcrumb-item>{{ goods.name }}</el-breadcrumb-item>

</el-breadcrumb>

</div>

--统计区域

<div class="media">

<!-- 图片预览区 -->

<XtxImageView :image-list="goods.mainPictures" />

<!-- 统计数量 -->

<ul class="goods-sales">

<li>

<p>销量人气</p>

<p> {{ goods.salesCount }}+ </p>

<p><i class="iconfont icon-task-filling"></i>销量人气</p>

</li>

<li>

<p>商品评价</p>

<p>{{ goods.commentCount }}+</p>

<p><i class="iconfont icon-comment-filling"></i>查看评价</p>

</li>

<li>

<p>收藏人气</p>

<p>{{ goods.collectCount }}+</p>

<p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>

</li>

<li>

<p>品牌信息</p>

<p>{{ goods.brand.name }}</p>

<p><i class="iconfont icon-dynamic-filling"></i>品牌主页</p>

</li>

</ul>

</div>

-- 商品详情

<div class="goods-article">

<!-- 商品详情 -->

<div class="goods-tabs">

<nav>

<a>商品详情</a>

</nav>

<div class="goods-detail">

<!-- 属性 -->

<ul class="attrs">

<li v-for="item in goods.details.properties" :key="item.value">

<span class="dt">{{ item.name }}</span>

<span class="dd">{{ item.value }}</span>

</li>

</ul>

<!-- 图片 -->

<img v-for="img in goods.details.pictures" :src="img" :key="img" alt="">

</div>

</div>

</div>

标签: #vue商品详情