1. 程式人生 > >4.vue的的點選事件點選繫結

4.vue的的點選事件點選繫結

1.js內容:
 //在使用vue之前必須例項化vue物件
new Vue({
    /*el:指element  需要獲取的元素,一定是html中根容器元素
        以後所有的操作均是在這個根容器中進行操作
    */
    el:"#vue-app",
    /*
    data:用於資料的儲存,用來寫屬性
        以key-value的值進行儲存
    */
    data:{
       age:30,
       x:0,
       y:0,
    },
    /**
     * 使用method來儲存方法,
     * 使用greet來進行呼叫
     * 使用this獲取當前容器的內容
    * this.age:就是指先找到當前容器的data然後在找到name對應的屬性
     */
    methods: {
        add:function(){
            this.age=age+1;
        },
          substract: function () {
            this.age--;
        },
        addUseParameter:function(n){
               this.age+=n; 
        },
          substrtractUseParameter: function (n) {
            this.age -= n;
        },
        updateXY: function (event) {
           this.x=event.offsetX;
            this.y = event.offsetY;
        }
    }
})
2.html內容:
   <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index.html</title>
    <!-- 用於線上引用vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引用css檔案 -->
    <link rel="stylesheet" href="../css/style.css">
    <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
</head>
<body>
   
    <!--vue-app就是我們的根容器,vue均是對vue操作-->
    <div id="vue-app">
      <h1>Events</h1>
     <!--使用v-on來繫結點選事件
    @和v-on的作用一樣
    -->
      <button @click:="add">加一歲</button>
      <!--此處是單擊事件-->
    <button v-on:click:="substract">減一歲</button>
  
    <!--此處是雙擊事件-->
    <button v-on:dblclick:="substract">減一歲</button>
    <button v-on:dblclick:="substract">加歲</button>
    <p>my Age:{{age}}</p>
    <div  v-on:mousemove="updateXY">
        {{x}},{{y}}
    </div>
    </div>
    <script src="../js/app.js"></script>
</body>
</html>