代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<script src="../js/vue.js"></script>
</head>
<body>
<div id="app">
<div>{{msg}}</div><button @click="ontime">start</button>
</div>
<script>
new Vue({
el: ‘#app‘,
data: { msg: 0 },
methods: {
ontime:function(){
setInterval(() => {
this.msg++;
}, 1000);
}
},
mounted: function () { this.ontime(); }
});
</script>
</body>
</html>
就是mounted要执行的函数必须要用function(){}包裹,然后填写在methods里面定义的函数
时间: 2024-10-13 15:03:14