vue生命周期-销毁流程

2022-07-05  本文已影响0人  tutututudou
lifecycle.png

vm.destroy()

vm销毁了.PNG
<div class="root" :n="n">
    <h1>让n加1:   {{n}}</h1>
    <button @click="add">点击加1</button>
    <button @click="quit">销毁了</button>
  </div>
  <script>
  Vue.config.devtools = true;
  const vm = new Vue({
    el:'.root',
    data:{
      n:1
    },
    methods:{
      add(){
        this.n++
      },
      quit(){
        this.$destroy()//销毁了,但是页面之前更新的数据不会被销毁,有残留
      }
    },
    beforeCreate(){
      console.log('beforeCreate')
      console.log(this)
      // debugger
    },
    created(){
      console.log('created')
      console.log(this)
      //debugger
    },
    beforMount(){
      console.log('beforeMount')
      console.log(this)
      // debugger
    },
    mounted(){
      console.log('mounted')
      console.log(this)
      // debugger
    },
    beforeUpdate(){
      console.log('beforeUpdate')
      console.log(this.n)
      // debugger
    },
    updated(){
      console.log('updated')
      console.log(this.n)
      // debugger
    }
  })
  // vm.$mount('.root')
  </script>

beforeDestroy、destroyed

  <div class="root" :n="n">
    <h1>让n加1:   {{n}}</h1>
    <button @click="add">点击加1</button>
    <button @click="quit">销毁了</button>
  </div>
  <script>
  Vue.config.devtools = true;
  const vm = new Vue({
    el:'.root',
    data:{
      n:1
    },
    methods:{
      add(){
        this.n++
      },
      quit(){
        this.$destroy()
      }
    },
    beforeCreate(){
      console.log('beforeCreate')
      console.log(this)
      // debugger
    },
    created(){
      console.log('created')
      console.log(this)
      //debugger
    },
    beforMount(){
      console.log('beforeMount')
      console.log(this)
      // debugger
    },
    mounted(){
      console.log('mounted')
      console.log(this)
      // debugger
    },
    beforeUpdate(){
      console.log('beforeUpdate')
      console.log(this.n)
      // debugger
    },
    updated(){
      console.log('updated')
      console.log(this.n)
      // debugger
    },
    beforeDestroy(){
      console.log('beforeDestroy')//点击销毁按钮
      console.log(this.n)// 不会触发数据更新
      // debugger
    },
    destroyed(){
      console.log('destroyed')//点击销毁按钮
      console.log(this.n)
      // debugger
    }
  })
  // vm.$mount('.root')
  </script>

上一篇 下一篇

猜你喜欢

热点阅读