1.生命周期.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <!--
  11. vue diff 算法 DOM虚拟树 DOM真实树
  12. 生命周期 => 生命周期钩子
  13. 三个阶段
  14. 1.初始阶段
  15. 创建前 beforeCreate 方法和数据是无法被使用的
  16. 创建后 created 方法和数据可以使用
  17. 2.运行阶段
  18. 挂载前 beforeMount 方法和数据是没有被挂载到页面上
  19. 挂载后 mounted 方法和数据是被挂载到页面上
  20. 更新前 beforeUpdate 方法和数据是没有被更新到页面上
  21. 更新后 updated 方法和数据是被更新到页面上
  22. 3.销毁阶段
  23. 销毁前 beforeDestroy 方法和数据是没有被销毁
  24. 销毁后 destroyed 方法和数据是被销毁
  25. -->
  26. <h1>{{msg}}</h1>
  27. <button v-on:click="getMsg">修改</button>
  28. <button v-on:click="getShow">展示</button>
  29. </div>
  30. <script src="../vue初阶/vue.js"></script>
  31. <script>
  32. var app = new Vue({
  33. el:"#app",
  34. data:{
  35. msg:"你好"
  36. },
  37. methods:{
  38. getMsg() {
  39. this.msg = "世界";
  40. },
  41. getShow() {
  42. }
  43. },
  44. beforeCreate() {
  45. // console.log(this.msg,this.$el,this.getMsg(),'1')
  46. },
  47. created() {
  48. // console.log(this.msg,this.$el,this.getMsg(),'2')
  49. },
  50. beforeMount() {
  51. console.log(this.msg,this.$el,'3')
  52. },
  53. mounted() {
  54. console.log(this.msg,this.$el,'4')
  55. },
  56. beforeUpdate() {
  57. console.log(this.msg,document.querySelector("h1").innerHTML,'5')
  58. },
  59. updated() {
  60. console.log(this.msg,document.querySelector("h1").innerHTML,'6')
  61. },
  62. beforeDestroy() {
  63. console.log(this.$el,'7')
  64. },
  65. destroyed() {
  66. console.log(this.$el,'8')
  67. },
  68. })
  69. </script>
  70. </body>
  71. </html>