生命周期.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. <!--
  8. 四个阶段:
  9. 初始:
  10. beforeCreate:data和methods不可用
  11. created:data和methods可用 DOM还未生成
  12. 挂载:
  13. beforeMount 模版已编译 但没有挂载到页面上
  14. mounted Dom已生成 可以操作Dom
  15. 更新:
  16. beforeUpdate 数据变化 Dom未更新
  17. updated 数据变化 Dom更新
  18. 销毁:
  19. beforeDestory 实例还能用
  20. destoryed 实例已销毁
  21. -->
  22. </head>
  23. <body>
  24. <div id="app">
  25. <h1>{{msg}}</h1>
  26. <button @click="changeMsg">修改</button>
  27. </div>
  28. <script src="../初阶/vue.js"></script>
  29. <script>
  30. var app = new Vue({
  31. el: "#app",
  32. data: {
  33. msg: "你好啊"
  34. },
  35. methods: {
  36. aa() {
  37. console.log("你好")
  38. },
  39. changeMsg() {
  40. this.msg = '哈哈哈'
  41. }
  42. },
  43. mounted() {
  44. console.log("挂载后", this.$el)
  45. },
  46. beforeMount() {
  47. console.log("挂载前", this.$el)
  48. },
  49. created() {
  50. console.log(this.$el)
  51. console.log("创建后", this.msg)
  52. },
  53. beforeCreate() {
  54. console.log(this.$el)
  55. console.log("创建前", this.msg)
  56. },
  57. beforeUpdate() {
  58. console.log("更新前",this.$el,document.querySelector("h1").innerText)
  59. },
  60. updated() {
  61. console.log("更新后",this.$el,document.querySelector("h1").innerText)
  62. },
  63. beforeDestory() {
  64. console.log("销毁前")
  65. },
  66. destoryed() {
  67. console.log("销毁后")
  68. }
  69. })
  70. </script>
  71. </body>
  72. </html>