언어/WEB

Vue - 컴포넌트 사용법

앨리스.W 2023. 9. 27. 16:33

html내장요소와 이름이 겹치지 않게 '-'사용
컴포넌트는 앱안의 미니앱임
자체 템플릿을 지정해야함

    <section id="app">
      <ul>
        <friend-contact></friend-contact>
      </ul>
    </section>
const app = Vue.createApp({
data() {
    return {
        friends: [
            {id: 'a1', 
            name: 'azy',
            phone: '123 456 789',
            email: 'zzzzz'},

            {id: 'a2', 
            name: 'jliy',
            phone: '123 456 000',
            email: 'yyyy'},
        ]
    }
}

});

app.component('friend-contact',{
    template:`
    <li>
        <h2>{{friend.name}}</h2>
        <button @click ="toggleDetail()">Show Details</button>
        <ul>
        <li><strong>Phone:</strong> {{friend.phone}}</li>
        <li><strong>Email:</strong> {{friend.email}}</li>
        </ul>
    </li>
    ` ,
    data() {
        return {
            detailvisible : false,
            friend: [
                {id: 'a1', 
                name: 'azy',
                phone: '123 456 789',
                email: 'zzzzz'}]
        
        };
    },
    methods : {
        toggleDetail() {
            this.detailvisible = !this.detailvisible;
        }
    },

});


app.mount('#app');
반응형

'언어 > WEB' 카테고리의 다른 글

Vue - $emit 쓰는 법  (0) 2023.09.29
Vue - props 컴포넌트간 통신하기  (0) 2023.09.28
Vue - templete,$ref, 생명주기  (0) 2023.09.27
vue v-if,v-else,v-show  (0) 2023.09.17
vue v-model, computed,watch,축약형,스타일 적용  (0) 2023.09.16