2.生命周期.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. 生命周期:
  12. 创建:
  13. beforeCreate 创建前:方法和数据是无法使用的
  14. created 创建后:方法和数据是可以使用的
  15. 运行:
  16. beforeMount 挂在前 虽然方法数据可以使用 但是真实页面未挂载
  17. mounted 挂在后 真实页面已经挂载
  18. beforeUpdate 更新前 值未改变
  19. updated 更新后 值已经改变
  20. 销毁:
  21. beforeDestroy 销毁前
  22. destroyed 销毁后
  23. -->
  24. <div id="first">{{msg}}</div>
  25. <button @click="change1">修改</button>
  26. </div>
  27. <script src="../vue初阶/vue.js"></script>
  28. <script>
  29. var vm = new Vue({
  30. el: "#app",
  31. data: {
  32. msg: "哈哈哈"
  33. },
  34. methods:{
  35. show() {
  36. console.log('ooo','ssss');
  37. },
  38. change1() {
  39. console.log('触发')
  40. this.msg = "嘿嘿嘿";
  41. }
  42. },
  43. beforeCreate() {
  44. console.log("创建前",this.msg);
  45. },
  46. created() {
  47. console.log("创建后",this.msg);
  48. },
  49. beforeMount() {
  50. console.log("挂载前",this.$el);
  51. this.show();
  52. },
  53. mounted() {
  54. console.log("挂载后",this.$el);
  55. },
  56. beforeUpdate() {
  57. console.log("更新前",this.$el,document.getElementById("first").innerHTML);
  58. },
  59. updated() {
  60. console.log("更新后",this.$el,document.getElementById("first").innerHTML);
  61. },
  62. beforeDestroy() {
  63. console.log("销毁前",document.getElementById("first").innerHTML,this.$el)
  64. },
  65. destroyed() {
  66. console.log("销毁后")
  67. },
  68. })
  69. </script>
  70. </body>
  71. </html>