Vue的计数器,跟react相比,仿佛开上了自动挡

zhuanbike 7月前 255

为什么我们喜欢vue,一样的效果,vue更加简洁,这里写一个计数器,比react少大约三分之二的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue</title>
   
</head>
<body>
      <div id="app">
        <div>
            <button @click="sub">-</button>
            <span>{{num}}</span>
            <button @click="add">+</button>
        </div>
      </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script>
        const vm = new Vue({
            el:"#app",
            data:{
                num:1
            },
            methods:{
                add:function(){  
                    this.num<=10?this.num++:alert('超过最大限额');
                },
                sub:function(){
                    this.num>0?this.num--:alert('已经最小了');
                }
            }            
        }
        )
    </script>
</body>
</html>


最新回复 (0)
发新帖