9_生命周期.html 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. {{count}}
  12. <button @click="add()">+1</button>
  13. <button @click="des()">销毁</button>
  14. </div>
  15. <script src="vue.js"></script>
  16. <script>
  17. var app = new Vue({
  18. el: '#app',
  19. data: {
  20. count: 1,
  21. mount: setInterval(() => {
  22. console.log(1)
  23. }, 1000)
  24. },
  25. methods: {
  26. add(){
  27. this.count++
  28. },
  29. des(){
  30. clearInterval(this.mount)
  31. this.$destroy()
  32. }
  33. },
  34. //组件实例刚刚创建之前 属性计算之前
  35. beforeCreate(){
  36. console.log('beforeCreate',this.$data,this.$el)
  37. },
  38. //组件实例刚刚创建完成 属性绑定 dom没有生成 el不存在
  39. created(){
  40. console.log('created',this.$data,this.$el)
  41. },
  42. //挂载前
  43. beforeMount(){
  44. console.log('beforeMount',this.$data,this.$el)
  45. },
  46. //挂载后
  47. mounted(){
  48. console.log('mounted',this.$data,this.$el)
  49. },
  50. beforeUpdate(){
  51. console.log('beforeUpdate',this.$data,this.$el)
  52. },
  53. updated(){
  54. console.log('updated',this.$data,this.$el)
  55. },
  56. beforeDestroy(){
  57. console.log('beforeDestroy',this.$data,this.$el)
  58. },
  59. destroyed(){
  60. console.log('destroyed',this.$data,this.$el)
  61. }
  62. })
  63. </script>
  64. </body>
  65. </html>