自学内容网 自学内容网

VUE学习笔记9__vue指令v-on参数传递

在这里插入图片描述

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style type="text/css">
    .vendingMachine{
        margin: 10px 0;
        border: 2px solid black;
        width: 250px;
        height: 100px;
        border-radius: 10px;
        box-shadow: 2px 2px 2px #ccc;
        text-align: center;
        /* display: flex;
        align-items: center;
        justify-content: center; */
    }    
    </style>
</head>

<body>
<div id="app">
    <div class="vendingMachine">
        <h3>小黑自动售货机</h3>
        <button @click="buy(5)">可乐5</button>
        <button @click="buy(10)">咖啡10</button>
        <button @click="buy(8)">牛奶8</button>
    </div>
    <p>银行卡余额:{{money}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    const app = new Vue({
            el: '#app',
            data: {
              money: 100
            },
            methods:{
                buy(price){
                    if (this.money<price){
                        alert("钱不够啦")
                    }else{
                        this.money -= price
                    }
                }
            }
          })
</script>
</body>
</html>

预览
在这里插入图片描述


原文地址:https://blog.csdn.net/houmenghu/article/details/145212322

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!