1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9 <script src="./lib/vue-2.4.0.js"></script> 10 </head> 11 12 <body> 13 <div id="app"> 14 <counter></counter> 15 <hr> 16 <counter></counter> 17 <hr> 18 <counter></counter> 19 </div> 20 21 22 <template id="tmpl"> 23 <div> 24 <input type="button" value="+1" @click="increment"> 25 <h3>{{count}}</h3> 26 </div> 27 </template> 28 29 <script> 30 var dataObj = { count: 0 } 31 32 // 這是一個計數器的組件, 身上有個按鈕,每當點擊按鈕,讓 data 中的 count 值 +1 33 Vue.component('counter', { 34 template: '#tmpl', 35 data: function () { 36 // return dataObj 37 return { count: 0 } 38 }, 39 methods: { 40 increment() { 41 this.count++ 42 } 43 } 44 }) 45 46 // 建立 Vue 實例,獲得 ViewModel 47 var vm = new Vue({ 48 el: '#app', 49 data: {}, 50 methods: {} 51 }); 52 </script> 53 </body> 54 55 </html>