我有一个products/index
页面和products/[slug]
在主要产品页面我有一个NuxtLink
更改页面,并前往products/[slug]
和获取数据,为这个产品。
在第一次点击我有一个正确的数据,但当我点击上一页或点击产品/索引页,然后尝试点击另一个产品,我有我点击的第一个产品的信息,在每一个产品,我再次有我点击的第一个产品的信息。
使用console.log
使我发现我的获取数据没有更改,我是否遗漏了什么?
在<nuxtPage/>
上使用:key
和:page-key
不起作用。
我单击的第一个产品
第二个产品仍然没有变化,数据来自第一个产品
第三张照片,仍然没有变化
productComponent.vue
<template>
<div class="mx-auto rounded border mb-7 border-blue-200 pt-1 px-2 mx-1 mb-2">
<NuxtLink :to="link">
<img
class="rounded mx-auto mb-3 border-b border-y-blue-300 pb-3"
:src="img"
:alt="alt"
/>
<div class="mt-2">
<div>
<div class="items-center font-bold text-slate-700 leading-snug">
<p class="pr-3">{{ title }}</p>
</div>
<div class="mt-2 text-lg text-slate-600 pr-3 pb-2">
قیمت : {{ price }} تومان
</div>
</div>
</div>
</NuxtLink>
</div>
</template>
<script>
export default {
name: 'ProductComponent',
props: ['title', 'price', 'img', 'alt', 'link'],
}
</script>
products/index.vue
页
<script setup>
useHead({
title: 'محصولات',
})
const {data, pending, refresh, error} = await useFetch('http://127.0.0.1:8000/api/products')
const products=data._value.data.data
</script>
<template>
<div>
<main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="relative z-10 flex items-baseline justify-between pt-24 pb-6 border-b mb-7 border-gray-200">
<h1 class="text-4xl font-extrabold tracking-tight ">محصولات</h1>
</div>
<div class=" border border-blue-300 mx-auto p-4 mb-7">
<div class="grid sm:grid-cols-2 md:grid-cols-2 gap-2 lg:grid-cols-3 xl:grid-cols-4">
<div v-for="(product,index) in products" :key="index">
<product-component :title="product.name"
:price="product.price"
:img="'http://127.0.0.1:8000/'+product.image[0].indexArray.large"
:alt="product.name"
:link="'/products/'+product.slug">
</product-component>
</div>
</div>
</div>
</main>
</div>
</template>
products/[slug]
<script setup>
const route = useRoute();
const {data: productData, pending, refresh, error} = await useFetch(`http://127.0.0.1:8000/api/products/${route.params.slug}`)
const product = productData._value.data[0]
console.log(product)
</script>
<template>
<div class="container mx-auto">
<section class="grid grid-cols-12 gap-3 mb-7 ">
<!-- little pic-->
<div class="md:col-span-1 mx-auto md:flex md:flex-wrap hidden overflow-auto" style="max-height: 36rem">
<div class="cursor-pointer bg-amber-100 max-h-9">
<div class="max-h-fit mb-3 " v-for="(myImage , index) in product.image" :key="index"
@click="switchImage('http://127.0.0.1:8000/'+myImage.indexArray.large)">
<img class="object-fill " style="width: 80px;height: 60px"
:src="'http://127.0.0.1:8000/'+myImage.indexArray.large"
:alt="myImage.alt">
</div>
</div>
</div>
<!-- end of little pic-->
<!-- pic-->
<div v-if="image" class="md:col-span-6 col-span-12 w-100 w-full max-w-full">
<img class=""
:src="image"
:alt="image.alt">
</div>
<div v-else class="md:col-span-6 col-span-12 ">
<img class="object-fill"
:src="'http://127.0.0.1:8000/'+product.image[0].indexArray.large"
alt="">
</div>
<!--end of pic-->
<div class="col-span-12 md:hidden">
<div class="cursor-pointer overflow-x-scroll">
<div class=" mb-3 inline py-1 " v-for="(myImage , index) in product.image" :key="index" @click="switchImage(index)">
<img class="object-fill inline p-1 overflow-x-scroll" style="width: 80px;height: 60px"
:src="myImage.url"
:alt="myImage.alt">
</div>
</div>
</div>
<!-- details-->
<div class="col-span-12 md:col-span-4">
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> نام محصول :</p>
<p>{{ product.name }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> کشور :</p>
<p>{{ product.country }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> جنس :</p>
<p>{{ product.material }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> سن :</p>
<p>{{ product.age }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> رنگ :</p>
<p>{{ product.color }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> وزن :</p>
<p>{{ product.weight }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> طول :</p>
<p>{{ product.length }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> عرض :</p>
<p>{{ product.width }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> ارتفاع :</p>
<p>{{ product.height }}</p>
</div>
<div class="flex justify-between border-b border-amber-200 p-1 mb-4">
<p> قیمت :</p>
<p>{{ product.price }}</p>
</div>
</div>
<!-- end of details-->
</section>
<div class="p-3 mb-7" style="background-color:beige;">
<ul class="flex flex-row md:space-x-6">
<li class="block py-2 pr-4 pl-3 text-black">
توضیحات
</li>
</ul>
</div>
<div class="mb-5 mx-auto whitespace-normal p-1 border-b border-amber-100-200 ">
<div v-html="product.description"></div>
</div>
<div class="mb-7">
<span class="block mb-4"> برچسب ها : </span>
<a v-for="(tags , index) in product.tags.split(',')" :title="tags" :key="index"
class="rounded-md text-sm" style="margin: 3px" rel="tag"
href="">{{ tags }} / </a>
</div>
</div>
</template>
<script>
export default {
data() {
return {
image: null,
}
},
methods: {
switchImage(index) {
this.image = index;
},
},
}
</script>
1条答案
按热度按时间gajydyqb1#
我找到了解决方案我必须在useFetch中添加一个参数
因为api缓存,不发送另一个请求,所以与
initialCache : false
它将发送另一个请求,这种方式是好的主要产品,但在产品/[slug]或博客/[slug]我认为你必须这样做,如果你有更好的方式请为我评论