传递数据 defineProps

vue3中使用defineProps来接收父组件传递过来的数据
不需要引入 defineProps

  1. // 父组件
  2. <headMessage
  3. :game-type="gameType"
  4. :group-name="yellowName"
  5. :score="yellowScore"
  6. :time="yellowTime"
  7. :pause-game="pauseGame"
  8. ></headMessage>
  9. //子组件接收数据
  10. const props = defineProps<{
  11. gameType: string,
  12. groupName: string,
  13. score: number,
  14. time: Array<number>,
  15. pauseGame: boolean,
  16. }>();
  17. //可以把数据结构出来
  18. const { gameType,groupName,score, time ,pauseGame} = reactive(props)
  19. //或者是直接调用
  20. console.log("队伍信息头部页面",props.gameType,props.groupName,props.score, props.time,props.pauseGame)

传递事件 defineEmits

不需要引入 defineEmits
子组件可以通过调用内置的[emit](https://staging-cn.vuejs.org/api/component-instance.html#emit)方法,通过传入事件名称来抛出一个事件:

  1. //子组件
  2. <template>
  3. <div class="blog-post">
  4. <h4>{{ title }}</h4>
  5. <button @click="next">Enlarge text</button>
  6. </div>
  7. </template>
  8. <script setup>
  9. //定义事件名,多个事件可以写到一个数组里
  10. const emit = defineEmits(["next-word","first-end","second-word"])
  11. //触发事件,
  12. const next = ()=>{
  13. //向上抛出
  14. emit('next-word')
  15. //也可以传递参数
  16. emit("first-end",'哈哈哈哈哈哈')
  17. }
  18. </script>
  19. //父组件
  20. <template>
  21. <headMessage
  22. @next-word="changeWord"
  23. @first-end="firstWordEndEvent"
  24. ></headMessage>
  25. </template>
  26. <script setup>
  27. //定义事件名,多个事件可以写到一个数组里
  28. const emit = defineEmits(["next-word","first-end","second-word"])
  29. //触发事件
  30. const changeWord = ()=>{
  31. console.log("下一个单词")
  32. }
  33. const firstWordEndEvent = (value)=>{
  34. console.log("能接收到参数======",value)
  35. }
  36. </script>