Props and Emit

https://kr.vuejs.org/v2/guide/components.html#Props

Props ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ

  • ๋ชจ๋“  component instance ์—๋Š” ์ž์ฒด ๊ฒฉ๋ฆฌ ๋œ ๋ฒ”์œ„๊ฐ€ ์žˆ๋‹ค

    • ์ฆ‰, ํ•˜์œ„ component์˜ template์—์„œ ์ƒ์œ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ฐธ์กฐ ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ๋Š” ์•ˆ ๋จ!

    • ๋ฐ์ดํ„ฐ๋Š” props option์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜์œ„ component๋กœ ์ „๋‹ฌ ๋  ์ˆ˜ ์žˆ๋‹ค

Prop

  • ์ƒ์œ„ component์˜ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ์œ„ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ํŠน์„ฑ

  • ํ•˜์œ„ component๋Š” props option์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์œ„ component๋กœ๋ถ€ํ„ฐ ๋ฐ›์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋˜๋Š” props ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•จ

ex)

Parent.vue - ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ

<template>
  <div class="parent">
      <h2>Parent Component</h2>
      <!-- 1. prop ์ด๋ฆ„="๋‚ด์šฉ" -->
      <Child @hungry="onHungrySignal" :propFromParent="parentMsg" />
      <!-- ์ž์‹์ด $emit ํ•œ custom event ๊ฐ€ hungry์ž„  -->
  </div>
</template>

<script>
// 1. import
import Child from '../components/Child.vue'

export default {
    name: 'Parent',
    data(){
        return {
            parentMsg: 'Message from parent',
        }
    },
    // 2. ๋“ฑ๋ก
    components: {
        Child, // Child: Child ๋ฅผ ๊ฐ„์†Œํ™” ํ•œ ๊ฒƒ์ž„!
    },
    methods: {
        onHungrySignal(menu1, menu2){
            console.log(menu1, menu2)
        }
    }
}
</script>

<style>

</style>

Child.vue - ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ

<template>
  <div class="child">
      <h2>Child Component</h2>
      <!-- 3. ์‚ฌ์šฉํ•œ๋‹ค. -->
      {{ propFromParent}}

      <button @click="hungrySignal"> ๋ฐฐ๊ณ ํŒŒ์š”!</button>
  </div>
</template>

<script>
export default {
    name: 'Child',
    // 2. props ๋“ฑ๋ก (๋ฐ˜๋“œ์‹œ object ๋ฅผ ์จ์•ผ Validation ๊ฐ€๋Šฅ)
    props: {
        propFromParent: String, //๋„˜์–ด์˜ค๋Š” data ์˜ validation ๊ฒ€์‚ฌ
    },
    methods: {
        hungrySignal () {
            // 1. ๋ถ€๋ชจํ•œํ…Œ ์ด๋ฒคํŠธ (์‹œ๊ทธ๋„) ๋ฐฉ์ถœ
            this.$emit('hungry', 'ํ”ผ์ž ๋จน๊ณ ์‹ถ๋‹ค', '์น˜ํ‚จ๋„ ๋จน๊ณ ์‹ถ๋‹ค') // Custom Event('์ด๋ฒคํŠธ์ด๋ฆ„', ...๋ฐ์ดํ„ฐ)
        }
    }
}
</script>

<style>
    .parent {
        border: 3px solid gray;
        margin: 3px;
        padding: 3px;
    }

    .child {
        border: 3px solid blue;
        margin: 3px;
        padding: 3px;
    }
</style>

camelCase vs kebab-case

  • HTML ์†์„ฑ์€ ๋Œ€์†Œ ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ๋•Œ camelCased prop ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” kebab-case๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

๋ฆฌํ„ฐ๋Ÿด vs ๋™์ 

  • ๋ฆฌํ„ฐ๋Ÿด prop์€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌ๋จ

    <comp some-prop="1"></comp>
  • JavaScript ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ•˜๋ ค๋ฉด ๊ฐ’์ด JavaScript ํ‘œํ˜„์‹์œผ๋กœ ํ‰๊ฐ€๋˜๋„๋ก v-bind or : ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ!

    <comp v-bind:some-prop="1"></comp>

๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„

  • ๋ชจ๋“  props๋Š” ํ•˜์œ„ ์†์„ฑ๊ณผ ์ƒ์œ„ ์†์„ฑ ์‚ฌ์ด์˜ ๋‹จ๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์„ ํ˜•์„ฑํ•จ

    • ์ƒ์œ„ ์†์„ฑ์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด ํ•˜์œ„๋กœ ํ๋ฅด๊ฒŒ ๋˜์ง€๋งŒ, ๊ทธ ๋ฐ˜๋Œ€๋Š” ์•ˆ ๋จ!

    • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹ค์ˆ˜๋กœ ๋ถ€๋ชจ์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์•ฑ์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ถ”๋ก ํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Œ

  • vue.js ๋Š” ์–‘๋ฐฉํ–ฅ ๊ฐ€๋Šฅ

    • v-model ์€ ์–‘๋ฐฉํ–ฅ

  • emit ํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€๋Šฅ

    • but, vue๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ„ ์–‘๋ฐฉํ–ฅ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋‹จ๋ฐฉํ–ฅ์„ ์“ฐ๋Š”๊ฒƒ์„ ๊ถŒ์žฅ!

      • ๋‹จ๋ฐฉํ–ฅ flow ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค!

Event Emit

์ปดํฌ๋„ŒํŠธ์˜ ํ†ต์‹  ๋ฐฉ๋ฒ• ์ค‘ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ์‹

์ด๋ฒคํŠธ ๋ฐœ์ƒ ์ฝ”๋“œ ํ˜•์‹

  • ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์˜ method๋‚˜ life-cycle hook ๊ณผ ๊ฐ™์€ ๊ณณ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

    this.$emit('EVENT_NAME');
  • ํ•ด๋‹น ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์œ„ component์˜ ํ…œํ”Œ๋ฆฟ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„

    <div id="app">
      <child-component v-on:์ด๋ฒคํŠธ ๋ช…="์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์˜ ์‹คํ–‰ํ•  ๋ฉ”์„œ๋“œ ๋ช… ๋˜๋Š” ์—ฐ์‚ฐ"></child-component>
    </div>

+

Youtube-browser ๋งŒ๋“ค๊ธฐ

1. Google Developer Console์—์„œ Youtube API key ๋ฐ›๊ธฐ

2. axios ์„ค์น˜

npm i axios

3. ๊ณต์‹ ๋ฌธ์„œ๋กœ ์‚ฌ์šฉ๋ฒ• ํŒŒ์•…ํ•˜๊ธฐ

https://developers.google.com/youtube/v3/docs/search

+

vue-filter

install

npm install @vuejs-community/vue-filter-date-parse

register

import Vue from 'vue';
import VueFilterDateParse from '@vuejs-community/vue-filter-date-parse';

Vue.use(VueFilterDateParse);

Last updated