- A+
所属分类:轻松一刻
七、Vue生命周期
Vue的生命周期指的是Vue实例在页面中创建到销毁整个过程。Vue提供了在各个生命周期的钩子,钩子也叫Vue生命周期函数。这些生命周期函数是伴随着Vue实例创建、销毁的过程中自动触发的(不需要人为手动触发)。Vue实例生命周期有三个阶段:
- 初始化阶段
- 运行阶段
- 销毁阶段
<template> <div> <p id="op" ref="myp">{{num}}</p> <button id="obtn" @click="btnClick">按钮</button> </div> </template> <script> export default { data() { return { num:20 }; }, methods:{ fn(){ return 999 }, btnClick(){ this.num++ } }, beforeCreate() { console.log("1.1---------------beforeCreate"); console.log(1.1, this.num, this.fn, document.getElementById("op")); }, created() { console.log("1.2-------------created"); console.log(1.2, this.num, this.fn, document.getElementById("op")); }, beforeMount() { console.log("2.1---------------beforeMount"); console.log(2.1, this.num, this.fn, document.getElementById("op")); }, mounted() { console.log("2.2-------------mounted"); console.log(2.2, this.num, this.fn, document.getElementById("op")); }, beforeUpdate(){ //视图跟新之前 console.log("3.1-------------beforeUpdate"); console.log(3.1, this.num, this.$refs.myp.innerHTML); }, updated(){ //视图跟新之后 console.log("3.2-------------updated"); console.log(3.2, this.num, this.$refs.myp.innerHTML); }, beforeDestory