Blogger Information
Blog 67
fans 0
comment 2
visits 71981
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
VUE3生命周期
搁浅
Original
1863 people have browsed it

VUE3运行生命周期

  1. <template>
  2. <div>
  3. {{msg}}
  4. </div>
  5. </template>
  6. <script>
  7. import MyBut from "./components/Mybut";<!--导入组件-->
  8. import AnLiu from "./components/AnLiu";
  9. export default {
  10. components(){<!--导入组件的名称-->
  11. MyBut,
  12. AnLiu
  13. }
  14. name: 'btu',
  15. data(){
  16. return{
  17. msg:'主键',
  18. num1:12,
  19. num2:5
  20. }
  21. },
  22. beforeCreate() {
  23. console.log('1在创建组件之前调用运行')
  24. },
  25. created() {
  26. console.log('2组件已经创建完成运行')
  27. },
  28. beforeMount() {
  29. console.log('3在模板挂在之前运行')
  30. },
  31. mounted() {
  32. console.log('4在模板挂完成以后运行')
  33. },
  34. beforeUpdate() {
  35. console.log('5在内容有改变之前运行')
  36. },
  37. updated() {
  38. console.log('6在数据改变完以后运行')
  39. },
  40. beforeUnmount() {
  41. console.log('7在组件销毁之前运行')
  42. },
  43. unmounted() {
  44. console.log('8组件销毁后执行')
  45. },
  46. activated() {
  47. console.log('缓存组件,激活时调用')
  48. },
  49. deactivated() {
  50. console.log('缓存组件,停用时调用')
  51. },
  52. watch:{
  53. console.log('监听属性,监听当前页面有变化,就执行watch。')
  54. num1(newVal, oldVal) {//第一个参数新的值,第二个参数旧值
  55. console.log("b--newVal: ", newVal, "b--oldVal: ",oldVal);
  56. }
  57. },
  58. methods:{ <!--在这里面写入function-->
  59. fun(){
  60. }
  61. },
  62. setup(){
  63. console.log('只执行一次')
  64. },
  65. computed:{ <!--计算属性-->
  66. num:{ <!--num相当于是data里面的属性,data里面和计算属性只能有一个-->
  67. get(){
  68. return this.num1+'-'+this.num2//只能读取数据。
  69. },
  70. set(){//如果更改了num,先执行set修改数据,在执行get读取数据
  71. this.tol=this.num1+'-'+this.num2
  72. }
  73. }
  74. num3:{//不写方法,默认get
  75. return this.num1+'-'+this.num2
  76. }
  77. }
  78. }
  79. </script>
  80. <style scoped lang="scss">
  81. </style>
第一次页面加载时会触发 beforeCreate, created, beforeMount, mounted 这几个钩子函数
setup() :开始创建组件之前,在beforeCreate和created之前执行。创建的是data和method
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
2 comments
搁浅 2022-04-22 16:53:54
补充执行顺序: 只执行一次 1在创建组件之前调用运行 2组件已经创建完成运行 3在模板挂在之前运行 4在模板挂完成以后运行
2 floor
搁浅 2022-02-22 20:29:52
补充:第一次页面加载时会触发 beforeCreate, created, beforeMount, mounted 这几个钩子函数
1 floor
Author's latest blog post