23.07.04(Vue.js)

MH S·2023년 7월 4일

Vue

목록 보기
2/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">
        <div v-if="liked">좋아요 눌렸음</div>
        <button v-else v-on:click="onClickButton()">like</button>
    </div>
</body>
<script>
    const app = new Vue({
        el: '#root',
        data: {
            liked: false,
        },
        methods: {
            onClickButton(){
                this.liked = true;
            }
        }
    });
</script>
</html>

결과


html 태그 안에 v- 로 시작하는 vue 코드가 있을시 그 안에는 script 코드를 선언할 수 있다.

구구단

<!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">
        <div>{{first}}곱하기 {{second}}는?</div>
        <form v-on:submit="onSubmitForm">
            <input type="number" ref="answer" v-model="value">
            <button type="submit">입력</button>
        </form>
        <div id="result">{{result}}</div>
    </div>
    <script>
        const app = new Vue({
            el: '#root',
            data: {
                first: Math.ceil(Math.random() * 9),
                second: Math.ceil(Math.random() * 9),
                value: '',
                result: '',
            },
            methods: {
                onSubmitForm(e){
                    e.preventDefault(); // 폼 새로고침 막음 
                    console.log(this);
                    if(this.first * this.second === parseInt(this.value)){
                        this.result = '정답';
                        this.first = Math.ceil(Math.random() * 9);
                        this.second = Math.ceil(Math.random() * 9);
                        this.value = '';
                        this.$refs.answer.focus();
                    } else{
                        this.result = '땡';
                        this.value = '';
                        this.$refs.answer.focus();
                    }
                }
            },
        });
    </script>
</body>
</html>

결과

끝말잇기

<!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">
        <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>
    <script>
        const app = new Vue({
            el: '#root',
            data: {
                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>
</body>
</html>

결과


0개의 댓글