1. ์ํ๋ณ ๋ฆฌ๋ทฐ ๋ทฐ์ด ๊ตฌํ
1) ์คํ ์ด์ reviews state ์ถ๊ฐ(store/index.js)
state: {
products: [
{
id: 0,
title: "๊ฐ์์ง ์ฌ๋ฃ",
},
{
id: 1,
title: "๊ณ ์์ด ์ฌ๋ฃ",
},
},
// reviews ์คํ
์ดํธ ์ถ๊ฐ
reviews: [
{
productId: 0,
reviewId : 0,
reviewtitle: "๊ฒฌ์ : ์ข์์!",
reviewer: "์กฐ",
reviewedate: "21.07.19",
},
{
productId: 0,
reviewId : 1,
reviewtitle: "๊ฐ๊ฐ ์ซ์ดํด์.",
reviewer: "๊น",
reviewedate: "21.07.19",
},
{
productId: 1,
reviewId : 2,
reviewtitle: "๊ณ ์์ด๊ฐ ์ ๋จน๋ค์!",
reviewer: "์ด",
reviewedate: "21.07.20",
},
],
}
2) ๋ฆฌ๋ทฐ ๋ทฐ์ด(ProductPage.vue)
<template>
<div id="productpage">
<div id="productdetail">
<b-card no-body>
<b-tabs card>
<b-tab no-body title="์ํ์ค๋ช
"></b-tab>
<b-tab no-body title="์์ธ์ ๋ณด"></b-tab>
<b-tab title="ํ๊ธฐ">
<ul>
<li><b-icon-check></b-icon-check>์ํ์ ๋ํ ๋ฌธ์๋ฅผ ๋จ๊ธฐ๋ ๊ณต๊ฐ์
๋๋ค.</li>
</ul>
<!-- ๋ฆฌ๋ทฐ ๋ทฐ์ด ํค๋ -->
<div class="review-item-header">
<div class="review-item-producttitle"> ์ ํ๋ช
</div>
<div class="review-item-reviewetitle"> ์ ๋ชฉ </div>
<div class="review-item-reviewer"> ์์ฑ์ </div>
<div class="review-item-reviewedate"> ์์ฑ์ผ </div>
</div>
<!-- /๋ฆฌ๋ทฐ ๋ทฐ์ด ํค๋ -->
<!-- ๋ฆฌ๋ทฐ ๋ทฐ์ด ์์ดํ
-->
<div
v-else
class="review-item"
v-for="review in $store.getters.getproductReview(product)"
>
<div class="review-item-producttitle">{{ product.title }}</div>
<div class="review-item-reviewetitle">{{ review.reviewtitle }}</div>
<div class="review-item-reviewer">{{ review.reviewer }}</div>
<div class="review-item-reviewedate">{{ review.reviewedate }}</div>
</div>
<div
v-if="$store.getters.getproductReview(product).length === 0"
class="review-item-none"
>
"{{product.title}}"์ํ์ ๋๊ธ์ด ์์ง ์์ต๋๋ค.
</div>
<!-- /๋ฆฌ๋ทฐ ๋ทฐ์ด ์์ดํ
-->
</b-tab>
</b-tabs>
</b-card>
</div>
</div>
</template>
<script>
import EditQuantity from "@/components/EditQuantity.vue";
export default {
name: "ProductPage",
components: {
EditQuantity
},
data() {
const index = this.$route.params.contentId;
return {
product: this.$store.state.products[index]
};
};
};
</script>
3) ์คํ ์ด์ getters ์ถ๊ฐ(store/index.js)
getters: {
getproductReview:(state) => (productItem) => {
return state.reviews.filter(reviewItem => reviewItem.productId === productItem.id);
},
},
- ProductPage.vue์์ $store.getters.getproductReview(product)๋ก ๋ฐ์ product์ธ์๋ฅผ ์ด์ฉํด id๋ฅผ ๊ฐ์ ธ์ด.
- reviews ์คํ ์ดํธ์์ productId์ product.id๊ฐ ๊ฐ์ ๋ฆฌ๋ทฐ๋ฅผ ์ฐพ์ ํํฐ๋งํ์ฌ ๋ฐํํ๋ getters๋ฅผ ์์ฑ.
2. ๊ฒฐ๊ณผ
- ๋ฆฌ๋ทฐ๊ฐ ์๋ ์ํ์ ๋ฆฌ๋ทฐ ๋ทฐ์ด ๊ฒฐ๊ณผ
- ๋ฆฌ๋ทฐ๊ฐ ์๋ ์ํ์ ๋ฆฌ๋ทฐ ๋ทฐ์ด ๊ฒฐ๊ณผ
'๐ก์น ํ๋ก์ ํธ > (ํ๋ก ํธ์๋) ๋ง์ผ์ปฌ๋ฆฌ - ํด๋ก ์ฝ๋ฉ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ ํ ๊ฒ์ ๊ธฐ๋ฅ ๊ตฌํ (0) | 2021.08.05 |
---|---|
์ฃผ์ ์ ๋ ฅ ๊ตฌํ(daum ์ฃผ์ api) (0) | 2021.08.04 |
vuex ์ฅ๋ฐ๊ตฌ๋ ๊ตฌํ (0) | 2021.07.18 |
vuex ๋ฐ์ดํฐ ์ ๋ฌ(์์->ํ์) (0) | 2021.07.17 |
vue์ ๋ถํธ์คํธ๋ฉ ์ ์ฉ (0) | 2021.07.16 |