๐Ÿ“• Language/Vue

[Age of Vue] Vue.js ์‹œ์ž‘ํ•˜๊ธฐ - 11. ์ปดํฌ๋„ŒํŠธ ์†Œ๊ฐœ/์ „์—ญ ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก ๋ฐ ์‹ค์Šต

a n u e 2022. 5. 1. 16:46

์ปดํฌ๋„ŒํŠธ?

์ปดํฌ๋„ŒํŠธ๋Š” ํ™”๋ฉด์˜ ์˜์—ญ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ๋ทฐ์˜ ๊ธฐ๋Šฅ. ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ํ™”๋ฉด์„ ๊ฐœ๋ฐœํ•˜๊ฒŒ ๋˜๋ฉด, ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ ๊ฐœ๋ฐœ์˜ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

 

์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Root ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋œ๋‹ค.

 

์ „์—ญ ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก

Vue.component('์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„', ์ปดํฌ๋„ŒํŠธ ๋‚ด์šฉ);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- ์•„๋ž˜์—์„œ ๋“ฑ๋กํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ ์šฉ์‹œํ‚ด-->
        <app-header></app-header>
        <app-content></app-content>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        //์ „์—ญ ์ปดํฌ๋„ŒํŠธ(์ปดํฌ๋„ŒํŠธ๋ฅผ ๋“ฑ๋ก)
        //app-header๋ผ๋Š” ํƒœ๊ทธ
        Vue.component('app-header', {
            //template : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ‘œํ˜„๋˜๋Š” ๋งˆํฌ์—…์ด๋‚˜ ์Šคํƒ€์ผ
            template : '<h1>Header1</h1>'
        });
        Vue.component('app-content', {
            template : '<h2>Header2</h2>'
        })
        new Vue({
                el : '#app' //#:id ์ฐพ์Œ
            });
    </script>
</body>
</html>