8_生命周期函数.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. <script src="./js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>{{message}}</h1>
  12. <button @click="changeMessage">改变消息</button>
  13. </div>
  14. <script>
  15. new Vue({
  16. el: '#app',
  17. data: {
  18. message:""
  19. },
  20. methods:{
  21. changeMessage(){
  22. this.message="Hello Vue!"
  23. },
  24. // 自定义函数一定要写在methods中
  25. loadPage(){
  26. console.log("页面已经加载")
  27. }
  28. },
  29. // 生命周期函数
  30. // beforeCreate 表示:准备创建Vue实例之前 执行
  31. beforeCreate(){
  32. console.log("beforeCreate")
  33. },
  34. // 创建Vue实例成功以后执行 但是页面内容还没有展示
  35. // 常用于页面刷新时候的数据获取
  36. created(){
  37. console.log("created")
  38. },
  39. // 在整个DOM挂载之前 把整个DOM解构整理好了 还没有渲染到页面中
  40. beforeMount(){
  41. console.log("beforeMount")
  42. },
  43. // 在整个DOM挂载完成后执行 页面内容已经渲染到了页面中
  44. // 常用于修改页面样式或者内容
  45. mounted(){
  46. console.log("mounted")
  47. // 调用方法加载页面
  48. // 调用自定义函数使用this.函数名称()
  49. this.loadPage()
  50. },
  51. // data中的值发生变化的时候触发
  52. // 在数据更新之前执行
  53. beforeUpdate(){
  54. console.log("beforeUpdate")
  55. },
  56. // 在数据更新完成后执行
  57. updated(){
  58. console.log("updated");
  59. },
  60. // 在Vue实例销毁之前执行 (一般为跳转页面之前)
  61. beforeDistroy(){
  62. console.log("beforeDistroy")
  63. },
  64. // 在Vue实例销毁完成后执行
  65. destroyed(){
  66. console.log("destroyed")
  67. }
  68. })
  69. </script>
  70. </body>
  71. </html>