Vue3-计数器的实现

106 阅读1分钟

Vue实现的步骤

  1. 创建对象{createApp},导包
import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  1. 创建div,等标签,一般用id号绑定
  2. 实现具体方法

具体增减器的实现

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div class="input-num">
            <button @click="sub">-</button>
            <span>{{num}}</span>
            <button @click="add">+</button>
        </div>
    </div>

    <script type="module">
        import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
        createApp({
            data() {
                return {
                    num: 1
                }
            },
            methods: {
                add() {
                    if (this.num > 10) {
                        alert("too big!!")
                    } else {
                        this.num += 1
                    }

                },
                sub() {
                    if (this.num <= 0) {
                        alert("too small!!")
                    } else {
                        this.num -= 1
                    }

                }

            }


        }).mount("#app")
    </script>
</body>

</html>