我是新的vue js,我有这个组件,我试图使另一个API调用单击其中一个列表选项。
<template>
<div>
<h1 class="text-center text-4xl font-bold">Our Product</h1>
<ul class="flex items-center justify-center gap-5">
<li class="cursor-pointer text-xl" @click="handleClick(1)">Clothes</li>
<li class="cursor-pointer text-xl" @click="handleClick(5)">Shoes</li>
<li class="cursor-pointer text-xl" @click="handleClick(2)">Watches</li>
<li class="cursor-pointer text-xl" @click="handleClick(4)">Furniture</li>
</ul>
<div class="grid grid-cols-3 gap-5">
<div v-for="store in stores" :key="store.category.id">
<div class="max-w-xl h-auto">
<img :src="store.images[0]" alt="img" />
</div>
</div>
</div>
</div>
</template>
<script>
import { computed, ref, watch } from "vue";
import Clothes from "../Products/Clothes.vue";
import Shoes from "../Products/Shoes.vue";
import axios from "axios";
export default {
components: {
Clothes,
Shoes,
},
props: [],
data() {
return {
stores: [],
errors: [],
product: ref(1), //Where product is the changeable value
};
},
methods: {
handleClick(params) {
this.product = params;
console.log(this.product);
},
},
//Here is the Api call.
async created() {
await axios
.get(
`https://api.escuelajs.co/api/v1/categories/${this.product}/products`
)
.then((res) => {
this.stores = res.data;
console.log(this.stores);
})
.catch((e) => {
console.log(this.errors.push(e));
});
},
我希望当我点击一个特定的产品,它会更改为我插入的号码,谢谢
2条答案
按热度按时间vxf3dgd41#
1.当你使用选项API时不要使用
ref
,你混淆了选项api和组合api(setup
函数)1.将API调用移到一个方法,并从
created
和handleClick
调用它oipij1gg2#
您需要等待同步调用: