Vue基础7之生命周期

生命周期

引出生命周期

请添加图片描述

用css animation实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <style>
    .normal{
      animation: use 2s linear infinite;
    }
    @keyframes use{
      to{
        opacity: 0;
      }
    }
  </style>
</head>
<body>
  <div id="app">
    <h2 class="normal">欢迎学习Vue</h2>
  </div>
</body>
</html>

用定时器实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2 :style="{opacity}">欢迎学习Vue</h2>
  </div>
</body>
<script>
    const vm=new Vue({
      el:"#app",
      data:{
        opacity:1
      }
    });

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

错误:用methods实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2 :style="{opacity}">欢迎学习Vue</h2>
    {{change()}}
  </div>
</body>
<script>
    const vm=new Vue({
      el:"#app",
      data:{
        opacity:1
      },
      methods:{
        change(){
          setInterval(()=>{
            this.opacity-=0.1
            if(this.opacity<=0) this.opacity=1
          })
        }
      }
    })
  </script>
</html>

请添加图片描述
原因:
请添加图片描述
所以上述方法不可行

使用生命周期函数mounted实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2 :style="{opacity}">欢迎学习Vue</h2>
    <h2 v-if="test">测试在Vue二次解析模板时,是不会调用mounted的</h2>
    <button @click="test=!test">测试是否重复调用mounted</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      opacity:1,
      test:false
    },
    //Vue完成模板的解析并把初始的真实的DOM元素放入页面后(挂载完毕)调用mounted
    mounted(){
      console.log("mounted被调用,其中this是:",this);
      setInterval(()=>{
        this.opacity-=0.1
        if(this.opacity<=0) this.opacity=1
      },200)
    }
  })
</script>
</html>

请添加图片描述

生命周期定义

生命周期:

  1. 又名:生命周期回调函数、生命周期函数、生命周期钩子。
  2. 是什么:Vue在关键时刻帮我们调用的一些特殊名称的函数。
  3. 生命周期函数的名字不可更改,但函数的具体内容是程序员根据需求编写的。
  4. 生命周期函数中的this指向是 vm 或 组件实例对象。

分析生命周期

请添加图片描述

挂载流程

beforeCreate()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      debugger
    },
    // created() {
    //   console.log("created,this._data:",this._data);
    //   debugger
    // },
    // beforeMount() {
    //   console.log("beforeMount");
    //   document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
    //   // debugger
    // },
    // mounted() {
    //   console.log("mounted");
    //   document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
    //   debugger
    // },
  })
</script>
</html>

请添加图片描述

created()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      // debugger
    },
    created() {
      console.log("created,this._data:",this._data);
      debugger
    },
    // beforeMount() {
    //   console.log("beforeMount");
    //   document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
    //   // debugger
    // },
    // mounted() {
    //   console.log("mounted");
    //   document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
    //   debugger
    // },
  })
</script>
</html>

请添加图片描述

beforeMount()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      // debugger
    },
    created() {
      console.log("created,this._data:",this._data);
      // debugger
    },
    beforeMount() {
      console.log("beforeMount");
      document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
      debugger
    },
    // mounted() {
    //   console.log("mounted");
    //   document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
    //   debugger
    // },
  })
</script>
</html>

请添加图片描述

mounted()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      // debugger
    },
    created() {
      console.log("created,this._data:",this._data);
      // debugger
    },
    beforeMount() {
      console.log("beforeMount");
      document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
      // debugger
    },
    mounted() {
      console.log("mounted");
      document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
      debugger
    },
  })
</script>
</html>

请添加图片描述

template的作用

没有加template时的效果:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app" :x="n">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      // debugger
    },
    created() {
      console.log("created,this._data:",this._data);
      // debugger
    },
    beforeMount() {
      console.log("beforeMount");
      // document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
      // debugger
    },
    mounted() {
      console.log("mounted");
      // document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
      // debugger
    },
  })
</script>
</html>

请添加图片描述
使用template时候的效果:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生命周期</title>
  <script src="Vuejs/vue.js"></script>
</head>
<body>
  <div id="app" :x="n">
    <div id="number">当前的n值是:{{n}}</div>
    <button @click="add">点我+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    template:"<div></button><div id='number'>当前的n值是:{{n}}</div><button @click='add'>点我+1</button></button></div>",
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate,this._data:",this._data);
      // debugger
    },
    created() {
      console.log("created,this._data:",this._data);
      // debugger
    },
    beforeMount() {
      console.log("beforeMount");
      // document.querySelector("#number").innerHTML="beforeMount修改数据不能停留,因为真实dom还没插入页面"
      // debugger
    },
    mounted() {
      console.log("mounted");
      // document.querySelector("#number").innerHTML="mounted修改数据可以停留在页面上,因为Vue已经解析完成了"
      // debugger
    },
  })
</script>
</html>

请添加图片描述
请添加图片描述

更新流程

beforeUpdate()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>更新流程</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2>当前的n值是:{{n}}</h2>
    <button @click="add">点我n+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate");
    },
    created() {
      console.log("created");
    },
    beforeMount() {
      console.log("beforeMount");
    },
    mounted() {
      console.log("mounted");
    },
    beforeUpdate() {
      console.log("beforeUpdated");
      console.log("this.n:",this.n);
      debugger
    },
  })
</script>
</html>

请添加图片描述

updated()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>更新流程</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2>当前的n值是:{{n}}</h2>
    <button @click="add">点我n+1</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        this.n++
      }
    },
    beforeCreate() {
      console.log("beforeCreate");
    },
    created() {
      console.log("created");
    },
    beforeMount() {
      console.log("beforeMount");
    },
    mounted() {
      console.log("mounted");
    },
    beforeUpdate() {
      console.log("beforeUpdated");
      // console.log("this.n:",this.n);
      // debugger
    },
    updated() {
      console.log("updated");
      console.log("this.n:",this.n);
      debugger
    },
  })
</script>
</html>

请添加图片描述

销毁流程

beforeDestroy()
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>example</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2>当前的n值是:{{n}}</h2>
    <button @click="add">点我n+1</button>
    <button @click="bye">点我销毁vm</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      n:1
    },
    methods: {
      add(){
        console.log("add");
        this.n++
      },
      bye(){
        console.log("bye");
        this.$destroy()
      }
    },
    beforeDestroy() {
      console.log("beforeDestroy");
      this.add()
    },
    destroyed() {
      console.log("destroyed");
    },
    
  })
</script>

请添加图片描述

生命周期总结

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>透明度案例</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h2 :style="{opacity}">欢迎学习Vue</h2>
    <button @click="stopS">点我停止变换</button>
  </div>
</body>
<script>
  new Vue({
    el:"#app",
    data:{
      opacity:1
    },
    methods: {
      stopS(){
        this.$destroy()
      }
    },
    mounted() {
      console.log("mounted");
      this.timer=setInterval(() => {
        console.log("setInterval");
        this.opacity-=0.01
        if(this.opacity<=0)  this.opacity=1
      },16)
    },
    beforeDestroy() {
      clearInterval(this.timer)
      console.log("vm即将驾鹤西去了");
    },
  })
</script>
</html>

请添加图片描述
常用的生命周期钩子:

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

关于销毁Vue实例:

  1. 销毁后借助用Vue开发者工具看不到任何信息。
  2. 销毁后自定义事件会失效,但原生DOM事件依然有效。
  3. 一般不会在beforeDestroy操作数据,因为即便操作数据,也不会再触发更新流程了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值