1. created() --组件创建时
    2. mounted() --组件被装载时
    3. updated() --data的数据更新,update生命周期函数会触发
    4. destroyed() --组件被销毁时 a-b b页面显示
    5. //初次加载会触发
    6. created() mounted()
    7. //a-b b页面会显示
    8. destroyed()
    9. //a-b b-a a页面会显示
    10. created() mounted()
    1. <template>
    2. <div class="about">
    3. <h1 ref="dom">This is an about page</h1>
    4. <input type="text" v-model="msg">
    5. </div>
    6. </template>
    7. <script>
    8. export default {
    9. name:"About",
    10. data(){
    11. return{
    12. msg:"hello world"
    13. }
    14. },
    15. beforeCreate(){
    16. console.log(this.msg)
    17. console.log("组件被创建之前")
    18. },
    19. created(){
    20. console.log(this.msg)
    21. console.log(this.$refs.dom)
    22. console.log("组件被创建好了")
    23. },
    24. /* 组件被装载到真实DOM元素之前 */
    25. beforeMount(){
    26. console.log("组件被装载之前")
    27. },
    28. mounted(){
    29. console.log("组件被装载到DOM上")
    30. window.addEventListener("scroll",this.go)
    31. },
    32. /* data的数据更新,update生命周期函数会触发 */
    33. beforeUpdate(){
    34. console.log("beforeUpdate")
    35. },
    36. updated(){
    37. console.log("updated")
    38. },
    39. beforeDestroy(){
    40. console.log("组件销毁之前")
    41. },
    42. destroyed(){
    43. console.log("组件销毁的时候")
    44. window.removeEventListener("scroll",this.go)
    45. },
    46. methods:{
    47. go(){
    48. console.log(1)
    49. }
    50. }
    51. }
    52. </script>
    53. <style scoped>
    54. </style>