9_生命周期.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. <button @click="fn()">+1</button>
  12. <button @click=des()>销毁</button>
  13. <h2 id="myCount">{{count}}</h2>
  14. </div>
  15. <script src="vue.js"></script>
  16. <script>
  17. new Vue({
  18. el:"#app",
  19. data:{
  20. count: 1
  21. },
  22. methods:{
  23. fn(){
  24. this.count++
  25. },
  26. des(){
  27. this.$destroy()
  28. }
  29. },
  30. //组件实例刚刚创建的时候 属性计算之前
  31. beforeCreate(){
  32. console.log('beforeCreate',this.$data,this.$el)
  33. },
  34. //组件实例刚刚创建完成 属性绑定上了 但是 dom未生成 所以el不存在
  35. created(){
  36. console.log('created',this.$data,this.$el)
  37. },
  38. //挂载之前
  39. beforeMount(){
  40. console.log('beforeMount',this.$data,this.$el)
  41. },
  42. //挂载之后
  43. mounted(){
  44. console.log('mounted',this.$data,this.$el)
  45. },
  46. beforeUpdate(){
  47. console.log('beforeUpdate',document.getElementById('myCount').innerHTML)
  48. },
  49. updated(){
  50. console.log('updated',document.getElementById('myCount').innerHTML)
  51. },
  52. beforeDestroy(){
  53. console.log('beforeDestroy')
  54. },
  55. destroyed(){
  56. console.log('destroyed')
  57. }
  58. })
  59. </script>
  60. </body>
  61. </html>