vue3.0组件传值的方式

vue3.0组件传值的方式

(一)父组件给子组件传值

1.父组件给子组件传值,子组件通过defineProps接收数据

// App.vue
<template>
 <div>
  <HelloWorld :data="data" name="我是邓紫棋"></HelloWorld>
 </div>
</template>
<script setup lang='ts'>
import { ref,reactive} from 'vue'
import HelloWorld from './components/HelloWorld.vue'
// vue3父组件给子组件传值
// 父组件通过v-bind绑定一个数据,然后子组件通过defineProps接受传过来的值
const data = reactive<number[]>([1,2,3])
</script>
// HelloWorld.vue
<template>
  <div>{{name}}</div>
  <div>{{data}}</div>
</template>
<script setup lang="ts">
import { ref ,onMounted} from 'vue'
 // 子组件接受值:通过defineProps 来接受 defineProps是无须引入的直接使用即可
 // 如果我们使用的TypeScript
 // 可以使用传递字面量类型的纯类型语法做为参数
 // 如 这是TS特有的
defineProps<{
   name:string,
   data:number[]
 }>()
// 如果不使用ts
// defineProps({
//     name:{
//         default:"",
//         type:string
//     },
//     data:Array
// })
</script>

2.Provide / Inject(依赖注入)
通常,当我们需要从父组件向子组件传递数据时,我们使用 props。想象一下这样的结构:有一些深度嵌套的组件,
而深层的子组件只需要父组件的部分内容。在这种情况下,如果仍然将 prop 沿着组件链逐级传递下去,可能会很麻烦。
provide 可以在祖先组件中指定我们想要提供给后代组件的数据或方法,而在任何后代组件中,我们都可以使用 inject 来接收 provide 提供的数据或方法

// App.vue
<template>
 <div>
  <HelloWorld/>
 </div>
</template>
<script setup lang='ts'>
import {ref,provide} from 'vue'
import HelloWorld from './components/HelloWorld.vue'
let falg =ref<number>(3)
provide('falg',falg) //注入
</script>
//HelloWorld.vue
<template>
<div>
      <div>{{data}}</div>
  </div>
</template>
<script setup lang="ts">
import { ref,inject} from "vue";
// 子组件注入父组件传的值
const data = inject('falg')
</script>
<style scoped>
</style>

(二)子组件给父组件传值

1.通过ref形式子组件给父组件传值,主要用defineExpose在子组件中把数据暴露

// HelloWorld.vue(子组件)
<template>

</template>
<script setup lang="ts">
import { ref,reactive} from 'vue'
const data = reactive<number[]>([4, 5, 6])
defineExpose({
    data
})
</script>
// App.vue(父组件)
<template>
 <div>
  <HelloWorld ref ='menus'></HelloWorld>
 </div>
</template>
<script setup lang='ts'>
import { ref,reactive,onMounted} from 'vue'
import HelloWorld from './components/HelloWorld.vue'
const menus = ref(null)

onMounted(()=>{
  console.log('我是子组件通过ref方式传过来的值',menus) 
})

</script>
<style scoped lang='scss'>
</style>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue 3.0中,父子组件之间的传值可以使用props和emit实现。props是父组件向子组件传递数据的一种方式,子组件通过声明props来接收父组件传递的值。父组件可以将数据通过属性的形式绑定到子组件上,子组件可以在props属性中声明接收该属性的类型和默认值。例如: 父组件代码: <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from "./ChildComponent.vue"; export default { components: { ChildComponent, }, data() { return { parentMessage: "Hello from parent", }; }, }; </script> 子组件代码: <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: { message: { type: String, default: "", }, }, }; </script> 在这个例子中,父组件传递了一个名为parentMessage的数据到子组件中,子组件使用props接收到父组件传递的message,并在模板中显示出来。 除了props,Vue 3.0还引入了新的API emit,它可以用于在子组件中向父组件发送消息。子组件可以通过$emit方法发送一个自定义事件,并可以传递需要发送的数据。父组件可以通过在子组件上监听该事件,并在回调中接收子组件发送的数据。例如: 子组件代码: <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit("messageSent", "Hello from child"); }, }, }; </script> 父组件代码: <template> <div> <child-component @messageSent="handleMessage"></child-component> </div> </template> <script> import ChildComponent from "./ChildComponent.vue"; export default { components: { ChildComponent, }, methods: { handleMessage(message) { console.log(message); }, }, }; </script> 在这个例子中,子组件中的按钮点击事件调用sendMessage方法,该方法使用$emit发送一个名为messageSent的自定义事件,并将"Hello from child"作为参数传递。父组件在模板中使用@messageSent监听该事件,并通过handleMessage方法处理接收到的消息。在handleMessage方法中,我们打印出了从子组件发送的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸥总

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值