No.63 Vue---Props组件交互、自定义事件组件交互、组件生命周期

一、Props组件交互

  • 组件与组件之间是需要存在交互的,否则完全没关系,组件的意义就很小了.
  • Prop是你可以在组件上注册的一些自定义 attribute.

 

二、自定义事件组件交互

  • 自定义事件可以在组件中反向传递数据,prop可以将数据从父组件传递到子组件,那么反向如何操作呢,就可以利用自定义事件实现 $emit .

 

三、组件生命周期

  • 每个组件在被创建时都要经过一系列的初始化过程一-例如,需要设置数据监听、编译模板、将实例挂载到DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会。

8个生命周期:

  • 这8个函数是自动化调用。

创建一个组件:MyComponent.vue

<template>
    <h3>生命周期函数</h3>
</template>

<script>
    export default {
        data(){
            return{message:""}
        },
        beforeCreate(){
            console.log ("beforeCreate:组件创建之前")
        },
        created(){
            console.log ("beforeCreate:组件创建完成")
        },
        beforeMount(){
            console.log ("beforeCreate:组件渲染之前")
        },
        mounted(){
            console.log ("beforeCreate:组件渲染完成")
        },
        beforeUnmount(){
            console.log ("beforeCreate:组件卸载之前")
        },
        unmounted(){
            console.log ("beforeCreate:组件卸载之后")
        }
 
    }
</script>

<style>

</style>

 App.vue文件中加载 :

<template>
  <img alt="Vue logo" src="./assets/1.png">
  <!-- <HelloWorld msg="Welcome to Your Vue.js App"/> -->
   <MyComponent />
</template>

<script>
import MyComponent from './components/MyComponent.vue'


// import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    // HelloWorld
    MyComponent
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

  

 

posted @ 2025-03-14 10:21  百里屠苏top  阅读(45)  评论(0)    收藏  举报