λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
[개발] Practice/Vue.js

[Vue.js] Instagram ν”„λ‘œμ νŠΈ μƒμ„±ν•˜κΈ° 및 λ°μ΄ν„°λ°”μΈλ”©ν•˜κΈ°

by Connecting-the-dots 2022. 4. 6.
728x90
λ°˜μ‘ν˜•

πŸ’‘ μ‹€μŠ΅ 포인트!

  • μ˜€λŠ˜μ€ Instagram ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜μ—¬ Instagram κ³Ό μœ μ‚¬ν•˜κ²Œ λ ˆμ΄μ•„μ›ƒμ„ λ§Œλ“€κ³  λ°μ΄ν„°λ°”μΈλ”©κΉŒμ§€ ν•΄λ³΄μ•˜λ‹€.
  • λ ˆμ΄μ•„μ›ƒμ€ 사싀상 κ°•μ‚¬λ‹˜μ΄ λ‹€ λ§Œλ“€μ–΄ μ˜€μ‹  거라... λ‚΄κ°€ ν•œ κ±°λΌκ³ λŠ” λ°μ΄ν„°νŒŒμΌ λ§Œλ“€κ³  λ°μ΄ν„°λ°”μΈλ”©ν•œ 것 μ™Έμ—λŠ” μ—†λ‹€.
  • λ‚˜μ€‘μ— λ ˆμ΄μ•„μ›ƒ λ§Œλ“œλŠ” 것도 ν˜Όμžμ„œ 진행해보면 μ‹œκ°„μ€ μ’€ 였래 κ±Έλ¦¬κ² μ§€λ§Œ μž¬λ―Έμžˆμ§€ μ•Šμ„κΉŒ 생각해본닀.

πŸ’œ Vue 둜 Instagram ν”„λ‘œμ νŠΈ μƒμ„±ν•˜κΈ°


πŸ’œ Instagram 처럼 λ ˆμ΄μ•„μ›ƒ λ§Œλ“€κΈ° 및 λ°μ΄ν„°λ°”μΈλ”©ν•˜κΈ°

🀍 데이터λ₯Ό 담은 posts.js 파일 λ§Œλ“€κΈ°

export default [
    {
      name: "Kim Hyun",
      userImage: "https://placeimg.com/100/100/arch",
      postImage: "https://placeimg.com/640/480/arch",
      likes: 36,
      date: "May 15",
      liked: false,
      content: "였늘 무엇을 ν–ˆλƒλ©΄μš” 아무것도 μ•ˆν–ˆμ–΄μš”",
      filter: "perpetua"
    },
    {
      name: "John Doe",
      userImage: "https://placeimg.com/200/200/people",
      postImage: "https://placeimg.com/640/480/people",
      likes: 20,
      date: "Apr 20",
      liked: false,
      content: "ν”ν•œ μžλž‘μŠ€νƒ€κ·Έλž¨",
      filter: "clarendon"
    },
    {
      name: "Minny",
      userImage: "https://placeimg.com/100/100/animals",
      postImage: "https://placeimg.com/640/480/animals",
      likes: 49,
      date: "Apr 4",
      liked: false,
      content: "μš°λ¦¬μ§‘ κ°œλŠ” ν™”μž₯μ‹€ 물도 λ‚΄λ¦Ό",
      filter: "lofi"
    }
  ]
  • post.js νŒŒμΌμ„ λ§Œλ“  λ‹€μŒ, κ°•μ‚¬λ‹˜μ΄ μ œκ³΅ν•΄μ£Όμ‹  데이터λ₯Ό λ‹΄μ•„ export default 문법을 μ‚¬μš©ν•˜μ—¬ λ‹€λ₯Έ νŒŒμΌμ—μ„œ import ν•  수 있게 λ§Œλ“€μ–΄μ£Όμ—ˆλ‹€.

🀍 App.vue

// 전체 μ½”λ“œ 미리보기
// 전체 μ½”λ“œμ˜ λ‚΄μš©μ΄ λ„ˆλ¬΄ κΈΈμ–΄, css 뢀뢄은 μ œμ™Έν–ˆλ‹€.

<template>
  <div class="header">
    <ul class="header-button-left">
      <li>Cancel</li>
    </ul>
    <ul class="header-button-right">
      <li>Next</li>
    </ul>
    <img src="./assets/logo.png" class="logo" />
  </div>

  <Container :posts="posts" />

  <div class="footer">
    <ul class="footer-button-plus">
      <input type="file" id="file" class="inputfile" />
      <label for="file" class="input-plus">+</label>
    </ul>
  </div>
</template>

<script>
import Container from "./components/Container.vue";
import posts from "./assets/posts";
export default {
  name: "App",
  data() {
    return {
      posts: posts,
    };
  },
  components: {
    Container: Container,
  },
};
</script>

<style>
</style>
  • posts.js μ—μ„œ 데이터듀을 posts λΌλŠ” μ΄λ¦„μœΌλ‘œ import ν•΄μ™€μ„œ data 보관함에 λ“±λ‘ν•΄μ£Όμ—ˆλ‹€.
  • 그리고 App.vue μ•ˆμ— Container.vue κ°€ 있고, 또 κ·Έ μ•ˆμ— Post.vue λ₯Ό 보여쀄 κ²ƒμ΄λ―€λ‘œ 일단 App.vue 에 Container.vue λ₯Ό import ν•˜κ³  components 에 등둝해쀀 λ‹€μŒ <template></template> μ•ˆμ˜ μ›ν•˜λŠ” μœ„μΉ˜μ— Container Component λ₯Ό λ„£μ–΄μ£Όμ—ˆλ‹€.
  • 그리고 posts λΌλŠ” λ°μ΄ν„°λŠ” μ΅œμ’…μ μœΌλ‘œ Post.vue κΉŒμ§€ μ „μ†‘λ˜μ–΄μ•Ό ν•˜λ―€λ‘œ, posts λΌλŠ” μ΄λ¦„μœΌλ‘œ data λ³΄κ΄€ν•¨μ˜ posts λ₯Ό μ „μ†‘ν•΄μ£Όμ—ˆλ‹€.

🀍 Container.vue

// 전체 μ½”λ“œ 미리보기
// 전체 μ½”λ“œμ˜ λ‚΄μš©μ΄ λ„ˆλ¬΄ κΈΈμ–΄, css 뢀뢄은 μ œμ™Έν–ˆλ‹€.

<template>
  <div>
    <Post :post="post" v-for="(post, i) in posts" :key="i" />
  </div>
</template>

<script>
import Post from "./Post.vue";
export default {
  name: "Container",
  data() {
    return {}
  },
  components: {
    Post: Post,
  },
  props: {
    posts: Array,
  },
};
</script>

<style>
</style>
  • Container.vue μ•ˆμ—λŠ” Post.vue κ°€ λ“€μ–΄κ°€μ•Ό ν•˜λ―€λ‘œ, λ¨Όμ € Post.vue λ₯Ό import ν•΄μ€€ 후에 components 에 λ“±λ‘ν•΄μ£Όμ—ˆλ‹€.
  • κ·Έ λ‹€μŒ <template></template> μ•ˆμ˜ μ›ν•˜λŠ” μœ„μΉ˜μ— Post Component λ₯Ό λ„£μ–΄μ£Όμ—ˆλ‹€.
  • 그리고, App.vue μ—μ„œ λ°›μ•„μ˜¨ posts λ₯Ό props 에 λ“±λ‘ν•œ ν›„ λ°˜λ³΅λ¬Έμ„ 톡해 posts 에 λ‹΄κΈ΄ 데이터 ν•˜λ‚˜ν•˜λ‚˜λ₯Ό post λΌλŠ” μ΄λ¦„μœΌλ‘œ λ‹€μ‹œ Post.vue 둜 μ „μ†‘ν•΄μ£Όμ—ˆλ‹€. 
    (μ—¬κΈ°λŠ” 아직 ν—·κ°ˆλ €μ„œ, 반볡문 λ§Œλ“œλŠ” μ—°μŠ΅μ„ ν•œ 후에 ν•˜λ‚˜μ”© μ½”λ“œλ₯Ό μˆ˜μ •ν•΄λ‚˜κ°”λ‹€.)

 


🀍 Post.vue

// 전체 μ½”λ“œ 미리보기
// 전체 μ½”λ“œμ˜ λ‚΄μš©μ΄ λ„ˆλ¬΄ κΈΈμ–΄, css 뢀뢄은 μ œμ™Έν–ˆλ‹€.

<template>
  <div class="post">
    <div class="post-header">
      <div
        class="profile"
        :style="{ backgroundImage: `url(${post.userImage})` }"
      ></div>
      <span class="profile-name">{{ post.name }}</span>
    </div>
    <div
      class="post-body"
      :style="{
        backgroundImage: `url(${post.postImage})`,
      }"
    ></div>
    <div class="post-content">
      <p>{{ post.likes }} Likes</p>
      <p>
        <strong>{{ post.name }}</strong> {{ post.content }}
      </p>
      <p class="date">{{ post.date }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  props: {
    post: Object,
  },
  components: {},
};
</script>

<style>
</style>
  • 이제 Container.vue μ—μ„œ props 둜 받은 post λ₯Ό props 에 λ“±λ‘ν•œ λ’€ ν•˜λ‚˜μ”© 데이터바인딩을 ν•΄μ£Όλ©΄ λœλ‹€.
  • λ‹€λ§Œ, style 속성에 λ°μ΄ν„°λ°”μΈλ”©ν•˜λŠ” 게 μ²˜μŒμ—λŠ” 쑰금 μ–΄λ €μ› λ‹€.
    • λ¨Όμ €, style 속성에 데이터바인딩을 ν•˜κΈ° μœ„ν•œ λ°‘μž‘μ—…μœΌλ‘œ style μ•žμ— 콜둠( : )을 λΆ™μ—¬μ£Όμ—ˆλ‹€.
    • μž‘μ„±ν•  λ•Œμ—λŠ” 객체 ꡬ문을 ν™œμš©ν•˜μ—¬ :style="{ backgroundImage: `url(${post.userImage})` }" 와 같이 μž‘μ„±ν•΄λ„ 되고, 일반적인 인라인 μŠ€νƒ€μΌ 속성 μž‘μ„±ν•˜λ“―μ΄ :style="`background-image: url(${post.userImage})`" 와 같이 μž‘μ„±ν•΄λ„ λœλ‹€.
    • 단, 객체 ꡬ문을 ν™œμš©ν•  λ•Œμ—λŠ” 속성λͺ…은 camelCase 와 kebab-case 둜 μž‘μ„±ν•΄μ•Ό ν•œλ‹€.

 

728x90
λ°˜μ‘ν˜•