vue父组件子组件传值、组件数据双向绑定、修饰符sync的使用

1、vue父组件传子组件

      父组件:<parent :content="data"></parent>

      子组件:<son></son> 中这样写

                    props: {

                               content

                   },

2、子组件传父组件

      在子组件中:

      this.$emit('update:visible', visible)

      父组件:

      <components  @update:visible="val=>getVisible=val"></components>

3、父组件子组件数据的双向绑定:

父组件:

      <components :visible="isVisible" @update:visible="val=>isVisible=val"></components>

      简写为<components :visible.sync="isVisible"></components>

子组件

      this.$emit('update:visible', visible)

 

 

 

 

     

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中,组件组件传值的方式有两种: 1. 使用 $emit 事件 在组件使用 `$emit` 方法触发一个自定义事件,并传递需要向组件传递的值,组件中通过监听该自定义事件来接收传递的值。例如: ```vue <template> <button @click="sendValueToParent">向组件传递值</button> </template> <script> export default { methods: { sendValueToParent() { this.$emit('send-value', '这是传递给组件的值'); } } } </script> ``` 在组件中监听组件的自定义事件,并接收组件传递的值: ```vue <template> <div> <child-component @send-value="receiveValueFromChild"></child-component> <p>从组件接收到的值:{{valueFromChild}}</p> </div> </template> <script> export default { data() { return { valueFromChild: '' } }, methods: { receiveValueFromChild(value) { this.valueFromChild = value; } } } </script> ``` 2. 使用 .sync 修饰符 通过在组件使用 `.sync` 修饰符,可以实现组件组件双向绑定传值。例如: ```vue <template> <div> <child-component :value.sync="valueFromParent"></child-component> <p>从组件接收到的值:{{valueFromParent}}</p> </div> </template> <script> export default { data() { return { valueFromParent: '' } } } </script> ``` 在组件中通过 `$emit` 方法触发一个自定义事件,组件使用 `.sync` 修饰符组件自定义事件转化为一个属性,来实现双向绑定传值: ```vue <template> <button @click="sendValueToParent">向组件传递值</button> </template> <script> export default { props: { value: { type: String, default: '' } }, methods: { sendValueToParent() { this.$emit('update:value', '这是传递给组件的值'); } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值