引出生命周期、生命周期_挂载流程、生命周期_更新流程、生命周期_销毁流程、生命周期_总结——Vue

11 篇文章 0 订阅

目录

一、引出生命周期

二、生命周期_挂载流程

三、生命周期_更新流程

四、生命周期_销毁流程

五、生命周期_总结

一、引出生命周期

生命周期:

    1.又名:生命周期回调函数、生命周期函数、生命周期钩子。

    2.是什么:Vue在关键时刻帮我们调用的一些特殊名称的函数

    3.生命周期函数的名字不可更改,但函数的具体内容是程序员根据需求编写的。

    4.生命周期函数中的this 指向vm组件实例对象

Vue完成模板的解析并把初始的真实的DOM元素放入页面后(挂载完毕)调用mounted

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>document</title>
	<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>	
</head>
<body>	
	
	<div id="root">
		<h2 v-if="a">你好啊</h2>
		<!-- {opacity}为{opacity: opacity}的简写形式,第一个opacity为属性,第二个为属性值 -->
		<h2 :style="{opacity}">欢迎学习Vue</h2>
		<!-- {{change()}} -->
	</div>

	<script type="text/javascript">	
		Vue.config.productionTip = false
	
		const vm = new Vue({
			el:'#root',
			data:{
				opacity:1,
				a:false,
			},
			methods:{
				/* change(){
					setInterval(()=>{
						this.opacity -= 0.01
						if(this.opacity <= 0) this.opacity = 1
					},16)
				}, */
			},
			// Vue完成模板的解析并把初始的真实的DOM元素放入页面后(挂载完毕)调用mounted
			mounted() {
				console.log('mounted',this);//此处的this是vue实例
				setInterval(()=>{
					vm.opacity -= 0.01
					if(vm.opacity <= 0) vm.opacity = 1
				},16)
			},			
		})

		// 通过外部的定时器实现(不推荐)
		/* setInterval(()=>{
			vm.opacity -= 0.01
			if(vm.opacity <= 0) vm.opacity = 1
		},16) */
	</script>
</body>
</html>

二、生命周期_挂载流程

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>

</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2 v-if="false">你好啊</h2>
        <h2 :style="{opacity}">欢迎学习 Vue</h2>
        <h2>当前的n值是:{{n}}</h2>
        <button @click="add">点我 n+1</button>
    </div>

    <script type="text/javascript">
        const vm = new Vue({
            el: '#root',
            // template: ` 
            //     <div>
            //         <h2>当前的n值是:{{n}}</h2>
            //         <button @click="add">点我 n+1</button>
            //     </div>
            // `,
            data: {
                opacity:1,
                n: 1
            },
            methods: {
                add(){
                    this.n++
                }
            },
            beforeCreate() {
                console.log('beforeCreate')
                // console.log(this);
                // debugger
            },
            created() {
                console.log('created')
                // console.log(this);
                // debugger
            },
            beforeMount() {
                console.log('beforeMount')
                // console.log(this);
                // debugger               
            },
            mounted(){
                console.log('mounted')
                // console.log(this);
                // debugger   
                /* setInterval(() => {
                    this.opacity -= 0.01
                    if (this.opacity < 0) {
                        this.opacity = 1
                    }
                }, 16) */
            }
       
        })    
     
    </script>
</body>
</html>

三、生命周期_更新流程

在哪个生命周期 钩子中页面与数据尚未不同步: beforeUpdate

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>

</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2 v-if="false">你好啊</h2>
        <h2 :style="{opacity}">欢迎学习 Vue</h2>
        <h2>当前的n值是:{{n}}</h2>
        <button @click="add">点我 n+1</button>
    </div>

    <script type="text/javascript">
        const vm = new Vue({
            el: '#root',
            // template: ` 
            //     <div>
            //         <h2>当前的n值是:{{n}}</h2>
            //         <button @click="add">点我 n+1</button>
            //     </div>
            // `,
            data: {
                opacity:1,
                n: 1
            },
            methods: {
                add(){
                    this.n++
                }
            },
            beforeCreate() {
                console.log('beforeCreate')
                // console.log(this);
                // debugger
            },
            created() {
                console.log('created')
                // console.log(this);
                // debugger
            },
            beforeMount() {
                console.log('beforeMount')
                // console.log(this);
                // debugger               
            },
            mounted(){
                console.log('mounted',this.$el,this.$el instanceof HTMLElement)
                // console.log(this);
                // debugger   
                /* setInterval(() => {
                    this.opacity -= 0.01
                    if (this.opacity < 0) {
                        this.opacity = 1
                    }
                }, 16) */
            },
            beforeUpdate() {
                console.log('beforeUpdate')
                // console.log(this.n);  //点击按钮 此时已变成 2
                // debugger
            },
            updated() {
                console.log('updated')
                debugger
            },
       
        })    
     
    </script>
</body>
</html>

四、生命周期_销毁流程

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>

</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2 v-if="false">你好啊</h2>
        <h2 :style="{opacity}">欢迎学习 Vue</h2>
        <h2>当前的n值是:{{n}}</h2>
        <button @click="add">点我 n+1</button>
        <button @click="bye">点我销毁vm</button>
    </div>

    <script type="text/javascript">
        const vm = new Vue({
            el: '#root',
            // template: ` 
            //     <div>
            //         <h2>当前的n值是:{{n}}</h2>
            //         <button @click="add">点我 n+1</button>
            //     </div>
            // `,
            data: {
                opacity:1,
                n: 1
            },
            methods: {
                add(){
                    console.log('add');
                    this.n++
                },
                bye(){
                    console.log('bye');
                    this.$destroy()
                }
            },
            beforeCreate() {
                console.log('beforeCreate')
                // console.log(this);
                // debugger
            },
            created() {
                console.log('created')
                // console.log(this);
                // debugger
            },
            beforeMount() {
                console.log('beforeMount')
                // console.log(this);
                // debugger               
            },
            mounted(){
                console.log('mounted',this.$el,this.$el instanceof HTMLElement)
                // console.log(this);
                // debugger   
                /* setInterval(() => {
                    this.opacity -= 0.01
                    if (this.opacity < 0) {
                        this.opacity = 1
                    }
                }, 16) */
            },
            beforeUpdate() {
                console.log('beforeUpdate')
                // console.log(this.n);  //点击按钮 此时已变成 2
                // debugger
            },
            updated() {
                console.log('updated')
                // debugger
            },
            beforeDestroy() {
                console.log('beforeDestroy')
                console.log(this.n)
                this.add()  //仍可使用 add,但对数据触发的操作不再更新,所以页面中 的n不会改变
            },
            destroyed() {
                console.log('destroyed')
            },
        })         
    </script>
</body>
</html>

五、生命周期_总结

上面一共讲了8 个生命周期,也就是4 对生命周期

beforeCreate 与 created  指数据检测与数据代理创建之前和之后

beforeMount 与 Mounted  

beforeUpdate 与 updated

beforeDestroy 与 destroy 

常用的生命周期钩子:
     1.mounted: 发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】。
     2.beforeDestroy: 清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】。

 关于销毁Vue实例
     1.  销毁后借助Vue开发者工具看不到任何信息。
     2.  销毁后自定义事件会失效,但原生DOM事件依然有效。

     3.  一般不会在beforeDestroy 操作数据,因为即便操作数据,也不会再触发更新流程了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>

</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="root">
        <!-- 需求:点击按钮停止变换 -->
        <h2 :style="{opacity}">欢迎学习 Vue</h2>
        <button @click="opacity = 1">透明度设置为1</button>
        <button @click="stop">停止变换</button>
    </div>

    <script type="text/javascript">

        const vm = new Vue({
            el: '#root',
            data: {
                opacity:1,
            },
            methods: {
                stop(){
                    
                    this.$destroy()
                }
            },
            mounted(){  
                this.timer = setInterval(() => {
                    this.opacity -= 0.01
                    console.log('定时器');
                    if (this.opacity < 0) {
                        this.opacity = 1
                    }
                }, 16)
            },
            beforeDestroy() {
                console.log();
                clearInterval(this.timer)
            },           
        })         
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值