๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ก์›น ํ”„๋กœ์ ํŠธ/(ํ”„๋ก ํŠธ์—”๋“œ) ๋งˆ์ผ“์ปฌ๋ฆฌ - ํด๋ก ์ฝ”๋”ฉ

vuex ์ƒํ’ˆ๋ณ„ ๋ฆฌ๋ทฐ ๋ทฐ์–ด

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. ๊ฒฐ๊ณผ

- ๋ฆฌ๋ทฐ๊ฐ€ ์—†๋Š” ์ƒํ’ˆ์˜ ๋ฆฌ๋ทฐ ๋ทฐ์–ด ๊ฒฐ๊ณผ

 

- ๋ฆฌ๋ทฐ๊ฐ€ ์žˆ๋Š” ์ƒํ’ˆ์˜ ๋ฆฌ๋ทฐ ๋ทฐ์–ด ๊ฒฐ๊ณผ