23.07.06(Vue.js)

MH S·2023년 7월 6일

Vue

목록 보기
3/8

컴포넌트

예제

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>끝말잇기</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="root">
        <word-relay></word-relay>
        <word-relay></word-relay>
        <word-relay></word-relay>
    </div>
    <script>
        Vue.component('word-relay',{ // 전역 컴포넌트
            template: `
                <div>
                    <div>{{word}}</div>
                        <form v-on:submit="onSubmitForm">
                            <input type="text" ref="answer" v-model="value">
                            <button type="submit">입력!</button>
                        </form>
                    <div>{{result}}</div>  
                </div>              
            `,
            data() {
                return {
                    word: '제로초',
                    result: '',
                    value: '',
                };
            },
            methods: {
                onSubmitForm(e){
                    e.preventDefault();
                    if(this.word[this.word.length -1] === this.value[0]){
                        this.result = '딩동댕';
                        this.word = this.value;
                        this.value = '';
                        this.$refs.answer.focus();
                    } else{
                        this.result = '땡';
                        this.value = '';s
                        this.$refs.answer.focus();
                    }                             
                },
            }
        })
    </script>
    <script>
        const app = new Vue({
            el: '#root',
        })
    </script>
</body>
</html>

결과

0개의 댓글