Vue3.0之使用context和父子组件传值

Vue3.0之使用context和父子组件传值

在 Vue3.0 中,setup 中可以有两个参数 第一个参数就是 props 第二个参数就是 context

context 解释

context.parent === this. p a r e n t c o n t e x t . r o o t = = = t h i s c o n t e x t . e m i t = = = t h i s . parent context.root === this context.emit === this. parentcontext.root===thiscontext.emit===this.emit
context.refs === this.$refs
context.slots === this.slots

在 context 里面可以选择 attrs,slots,emit

export default {
  setup(props, context) {
    // Attributes (Non-reactive object)
    console.log(context.attrs)

    // Slots (Non-reactive object)
    console.log(context.slots)

    // Emit Events (Method)
    console.log(context.emit)
  },
}
实例-在 context 使用 emit
1、子组件 利用 emit 发射出去

H1.vue

<template>
  <div>
    <div>{{name}}</div>
    <div>{{message}}</div>
  </div>
</template>

<script>
  import { toRefs } from 'vue'
  export default {
    props: { name: String },
    setup(props, context) {
      const { name } = toRefs(props)
      let message = name.value + '今天天气不错'
      //把数据发送走
      context.emit('fashe', message)
      return {
        message,
      }
    },
  }
</script>

<style lang="less" scoped></style>
2、父组件监听-必须要把监听的函数暴露出去

index.vue

<template>
  <div>
    <span>{{count}}</span>
    <span>{{double}}</span>
    <div @click="changecount">点击增加</div>
    <HComponent :name="data2" @fashe="shoudao"></HComponent>
    <div>{{data3}}</div>
  </div>
</template>

<script>
  import { ref, computed, reactive, toRefs, onMounted, watch } from 'vue' //ref接收一个参数 返回一个响应式对象  //computed是一个函数接收一个参数是函数

  import HComponent from './H1.vue'
  export default {
    components: {
      HComponent,
    },
    setup() {
      onMounted(() => {
        console.log('存在了')
      })
      const data = reactive({
        count: 0,
        double: computed(() => {
          return data.count * 2
        }),
        changecount: () => {
          data.count++
        },
      })
      let data2 = ref('哈哈,')
      let data3 = ref('接收的数据是:')
      watch(data2, (newval, oldval) => {
        console.log(data2)
        console.log(newval)
        console.log(oldval)
      })
      function shoudao(content) {
        data3.value = data3.value + content
        console.log(data3)
      }
      const result = toRefs(data)
      return {
        ...result,
        data2,
        data3,
        shoudao,
      }
    },
  }
</script>

<style lang="less" scoped></style>
  • 1
    点赞
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值