1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > vue 生命周期钩子函数

vue 生命周期钩子函数

时间:2022-12-31 01:09:32

相关推荐

vue 生命周期钩子函数

生命周期

每个 Vue 实例在被创建时都要经过一系列的初始化过程 :创建实例,装载模板,渲染模板等等。Vue 为生命周期中的每个状态都设置了钩子函数(监听函数)。每当 Vue 实例处于不同的生命周期时,对应的函数就会被触发调用。

钩子函数

 beforeCreated:我们在用 Vue 时都要进行实例化,因此,该函数就是在 Vue 实例化时调

用,也可以将他理解为初始化函数比较方便一点,在 Vue1.0 时,这个函数的名字就是

init。

 created:在创建实例之后进行调用。

 beforeMount:页面加载完成,没有渲染。如:此时页面还是{{name}}

 mounted:我们可以将他理解为原生 js 中的 window.οnlοad=function({.,.}),或许大家也在

用 jquery,所以也可以理解为 jquery 中的$(document).ready(function(){….}),他的功能就

是:在 dom 文档渲染完毕之后将要执行的函数,该函数在 Vue1.0 版本中名字为

compiled。 此时页面中的{{name}}已被渲染成张三

 beforeDestroy:该函数将在销毁实例前进行调用 。

 destroyed:改函数将在销毁实例时进行调用。

 beforeUpdate:组件更新之前。

 updated:组件更新之后。

<body><div id="app"><span id="num">{{num}}</span><button v-on:click="num++">点赞</button><h2>{{name}},有{{num}}个人点赞。</h2></div><script>let app = new Vue({el: "#app",data: {name: "张三",num: 100},methods: {show() {return this.name;},add() {this.num++;}},beforeCreate() {console.log("=========beforeCreate=============");console.log("数据模型未加载:" + this.name, this.num);console.log("方法未加载:" + this.show());console.log("html 模板未加载:" + document.getElementById("num"));},created: function () {console.log("=========created=============");console.log("数据模型已加载:" + this.name, this.num);console.log("方法已加载:" + this.show());console.log("html 模板已加载:" + document.getElementById("num"));console.log("html 模板未渲染:" + document.getElementById("num").innerText);},beforeMount() {console.log("=========beforeMount=============");console.log("html 模板未渲染:" + document.getElementById("num").innerText);},mounted() {console.log("=========mounted=============");console.log("html 模板已渲染: " + document.getElementById("num").innerText);},beforeUpdate() {console.log("=========beforeUpdate=============");console.log("数据模型已更新:" + this.num);console.log("html 模板未更新:" + document.getElementById("num").innerText);},updated() {console.log("=========updated=============");console.log("数据模型已更新:" + this.num);console.log("html 模板已更新:" + document.getElementById("num").innerText);}});</script></body>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。