I am a novice. I bought a small example in Liang Hao"s vue.js,. It"s about this. I hope the gods will answer.
complete code in body, why not use var _ this = this, this this is to point to the vue, the code / / declare a variable pointing to the Vue instance this, to ensure scope consistency, what does it mean? I tried it, and if I didn"t write var _ this = this, I just wrote it as follows,
mounted: function () {
this.timer = setInterval(function () {
this.data = new Date()
},1000)
},
,. .
< body >
<div id="app">
{{data}}
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script type="text/javascript">
var app = new Vue({
el: "-sharpapp",
data () {
return {
data: new Date(),
}
},
mounted: function () {
var _this = this // Vue this,
this.timer = setInterval(function () {
_this.data = new Date()
},1000)
},
beforeDestroy: function () {
if (this.timer) {
clearInterval(this.timer)
}
}
})
</script>
< / body >