vue.js执行mounted的实例
代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title></title> 6 <meta charset="utf-8" /> 7 <script src="../js/vue.js"></script> 8 </head> 9 <body> 10 <div id="app"> 11 <div>{{msg}}</div><button @click="ontime">start</button> 12 </div> 13 14 <script> 15 new Vue({ 16 el: '#app', 17 data: { msg: 0 }, 18 methods: { 19 ontime:function(){ 20 setInterval(() => { 21 this.msg++; 22 }, 1000); 23 24 } 25 }, 26 mounted: function () { this.ontime(); } 27 }); 28 </script> 29 </body> 30 </html>
就是mounted要执行的函数必须要用function(){}包裹,然后填写在methods里面定义的函数