๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/Vue.js

[Vue.js] import / export ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ

by Connecting-the-dots 2022. 4. 1.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • data.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์ค€ ๋‹ค์Œ import / export ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ App.vue ํŒŒ์ผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ์„ ํ•ด๋ณด์•˜๋‹ค.
  • ์ด์ œ ๊ทธ ๋‹ค์Œ์—๋Š” ๋ชจ๋‹ฌ์ฐฝ์— ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ์„ ์ง„ํ–‰ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.
  • ๊ทธ๋‚˜์ €๋‚˜ ํ—ˆ์œ„๋งค๋ฌผ์‹ ๊ณ  ๋ฒ„ํŠผ์— ์™œ ์ปค์„œ๋ฅผ ์˜ฌ๋ฆฌ๊ธฐ๋งŒ ํ•˜๊ณ  ํด๋ฆญ์„ ์•ˆํ–ˆ์ง€..? ๊ดœํžˆ ๊ธฐ๋Šฅ ์ž‘๋™ ์•ˆํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ... 

๐Ÿ’œ ์‚ฌ์šฉํ•  ์ƒํ’ˆ ๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

[{
  id : 0,
  title: "Sinrim station 30 meters away",
  image: "https://codingapple1.github.io/vue/room0.jpg",
  content: "18๋…„ ์‹ ์ถ•๊ณต์‚ฌํ•œ ๋‚จํ–ฅ ์›๋ฃธ โ˜€๏ธ, ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ๊ณต",
  price: 340000
  },
  {
  id : 1,
  title: "Changdong Aurora Bedroom(Queen-size)",
  image: "https://codingapple1.github.io/vue/room1.jpg",
  content: "์นจ์‹ค๋งŒ ๋”ฐ๋กœ ์žˆ๋Š” ๊ณต์šฉ ์…ฐ์–ดํ•˜์šฐ์Šค์ž…๋‹ˆ๋‹ค. ์ตœ๋Œ€ 2์ธ ๊ฐ€๋Šฅ",
  price: 450000
  },
  {
  id : 2,
  title: "Geumsan Apartment Flat",
  image: "https://codingapple1.github.io/vue/room2.jpg",
  content: "๊ธˆ์‚ฐ์˜ค๊ฑฐ๋ฆฌ ์—ญ์„ธ๊ถŒ ์•„ํŒŒํŠธ์ž…๋‹ˆ๋‹ค. ์• ์™„๋™๋ฌผ ๋ถˆ๊ฐ€๋Šฅ ?",
  price: 780000
  },
  {
  id : 3,
  title: "Double styled beds Studio Apt",
  image: "https://codingapple1.github.io/vue/room3.jpg",
  content: "๋ฌด์•”๋™์ธ๊ทผ 2์ธ์šฉ ์›๋ฃธ์ž…๋‹ˆ๋‹ค. ์ „์„ธ ์ „ํ™˜๊ฐ€๋Šฅ",
  price: 550000
  },
  {
  id : 4,
  title: "MyeongIl Apartment flat",
  image: "https://codingapple1.github.io/vue/room4.jpg",
  content: "ํƒ„์ฒœ๋™ ์•„ํŒŒํŠธ ์›”์„ธ, ๋‚จํ–ฅ, ์—ญ 5๋ถ„๊ฑฐ๋ฆฌ, ํ—ˆ์œ„๋งค๋ฌผ์•„๋‹˜",
  price: 680000
  },
  {
  id : 5,
  title: "Banziha One Room",
  image: "https://codingapple1.github.io/vue/room5.jpg",
  content: "๋ฐ˜์ง€ํ•˜ ์›๋ฃธ์ž…๋‹ˆ๋‹ค. ๋น„์˜ฌ ๋•Œ ๋ฌผ๊ฐ€๋” ์ƒˆ๋Š”๊ฑฐ ๋นผ๋ฉด ์ข‹์•„์š”",
  price: 370000
}];
  • ์„œ๋ฒ„๊ฐ€ ์žˆ์œผ๋ฉด ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•˜์ง€๋งŒ, ์„œ๋ฒ„ ์—†์ด ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ๊ฐ•์‚ฌ๋‹˜์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ด์˜ค์…จ๋‹ค.
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์œผ๋ฉด data ๋ณด๊ด€ํ•จ์— ๋ฐ”๋กœ ๋„ฃ์–ด์ฃผ์–ด๋„ ๋˜์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋Š” ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ์—๋Š” import / export ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ js ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ๋’ค ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

๐Ÿ’œ import / export ๋ฌธ๋ฒ• ์‚ฌ์šฉํ•˜๊ธฐ

  • ๋ณดํ†ต ํŠน์ • JavaScript ํŒŒ์ผ์—์„œ ๋งŒ๋“  ๋ณ€์ˆ˜๋‚˜ ์ž๋ฃŒ๋ฅผ ๋‹ค๋ฅธ JavaScript ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ import ์™€ export ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ผ๋‹จ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ์ž๋ฃŒ๊ฐ€ ๋‹ด๊ธด ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ export ๋ฅผ ํ•ด์•ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ํŒŒ์ผ์—์„œ import ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • React ์—์„œ import ์™€ export ๋ฅผ ๊ณต๋ถ€ํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด vue ์—์„œ๋„ import / export ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋˜‘๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•œ๋‹ค.
    (๊ทธ๋ž˜๋„ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด React ๊ณต๋ถ€ํ•  ๋•Œ ์ •๋ฆฌํ•ด๋†“์€ ์ž๋ฃŒ๊ฐ€ ์žˆ์œผ๋‹ˆ ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญ!)

๐Ÿค import ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ data ๋ณด๊ด€ํ•จ์— ๋“ฑ๋กํ•˜๊ธฐ

// data.js ์— ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ roomData ๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ import ํ•ด์ฃผ์—ˆ๋‹ค.
import roomData from './assets/data'


// data ๋ณด๊ด€ํ•จ์—๋Š” import ํ•œ roomData ๋ฅผ rooms ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
data(){
    return {
        openModal : false,
        navbar : ['Home', 'Rooms', 'About'],
        rooms : roomData,
        report : [...roomData].fill(0),
    }
},
  • ๋‚˜๋Š” data.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ํ›„ roomData ๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ export ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  App.vue ํŒŒ์ผ์—์„œ import ํ•  ๋•Œ์—๋„ roomData ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ import ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ด์ œ import ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด data ๋ณด๊ด€ํ•จ์— ๋“ฑ๋กํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•œ๋ฐ, ๋‚˜๋Š” roomData ๋ฅผ rooms ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ data ๋ณด๊ด€ํ•จ์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ ํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์‹ ๊ณ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์‹ ๊ณ  ์ˆ˜๋ฅผ ์ €์žฅํ•ด๋‘๋Š” report ๋ถ€๋ถ„์€ roomData ๊ฐ€ ๋ณ€๊ฒฝ์ด ๋  ๋•Œ๋งˆ๋‹ค ์ˆ˜์ •์„ ํ•ด์ค„ ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์—, roomData ์˜ ์‚ฌ๋ณธ์„ ๋งŒ๋“ค์–ด ๋ชจ๋“  ๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก spread ๋ฌธ๋ฒ•๊ณผ .fill() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.
    (์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋  ๋•Œ ๊ธฐ์กด์˜ ๊ธฐ๋Šฅ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๊ทธ ๋•Œ ๊ทธ ๋•Œ ์ˆ˜์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.)

๐Ÿค data ๋ณด๊ด€ํ•จ์— ๋“ฑ๋กํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ

{
    id : 0,
    title: "Sinrim station 30 meters away",
    image: "https://codingapple1.github.io/vue/room0.jpg",
    content: "18๋…„ ์‹ ์ถ•๊ณต์‚ฌํ•œ ๋‚จํ–ฅ ์›๋ฃธ โ˜€๏ธ, ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ œ๊ณต",
    price: 340000
}

 

<div class="card" v-for="(room, i) in rooms" :key="i">
    <img :src="room.image" class="room-image">
    <h4 @click="openModal = true" class="room-name">{{ room.title }}</h4>
    <p>{{ room.price }}์›</p>
    <span>์‹ ๊ณ  ์ˆ˜: {{ report[room.id] }}</span> <button class="report" @click="increase(room.id)">ํ—ˆ์œ„๋งค๋ฌผ์‹ ๊ณ </button>
</div>
  • ๊ธฐ์กด์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜์—ฌ ์ž‘์„ฑํ•ด๋‘์—ˆ๋˜ html ์ฝ”๋“œ๋ฅผ ๋จผ์ € v-for ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
    (๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋งŒ๋“œ๋Š” ์ž์„ธํ•œ ๊ณผ์ •์€ ์ƒ๋žตํ•œ๋‹ค. ๊ทธ๋ƒฅ ๊ทœ์น™ ์ฐพ์•„์„œ ์ค„์—ฌ๋ฒ„๋ฆฌ๋ฉด OK!)
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉ์„ ์ง„ํ–‰ํ•  ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์„ ์ฐธ๊ณ ํ•˜์—ฌ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•