vue父子组件传参(子组件调用父组件方法,父组件调用子组件方法或属性,子组件修改prop值)

一、父组件向子组件传递参数

父组件通过在子组件的标签上使用v-bind或简写的:语法,将父组件的parentMessage作为message的值传递给子组件。子组件通过定义props接收传递的参数,并在模板中渲染该参数的值。

<!-- 父组件 -->
<template>
  <div>
    <h2>父组件</h2>
    <ChildComponent :message="parentMessage" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
  data() {
    return {
      parentMessage: 'Hello from parent component',
    };
  },
};
</script>
<!-- 子组件 ChildComponent.vue -->
<template>
  <div>
    <h3>子组件</h3>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  props: {
    message: String,
  },
};
</script>

二、vue子组件调用父组件的方法

子组件通过按钮的点击事件@click来调用triggerEvent方法,在该方法中使用this.$emit触发了名称为child-event的自定义事件。

在父组件中,使用@child-event绑定父组件的parentMethod方法来监听子组件触发的自定义事件。

在子组件的emit事件中,可以传递额外的参数,供父组件的方法使用。例如:this.$emit('child-event', data),其中data是传递的参数,父组件的方法可以通过监听事件时的回调函数接收这些参数。

<!-- 父组件 -->
<template>
  <div>
    <h2>父组件</h2>
    <ChildComponent @child-event="parentMethod" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
  methods: {
    parentMethod() {
      console.log('Method called from child component');
    },
  },
};
</script>
<!-- 子组件 ChildComponent.vue -->
<template>
  <div>
    <h3>子组件</h3>
    <button @click="triggerEvent">触发父组件方法</button>
  </div>
</template>

<script>
export default {
  methods: {
    triggerEvent() {
      // 不带参
      this.$emit('child-event'); // 触发父组件的自定义事件
      // 带参
      this.$emit('child-event''222'); // 触发父组件的自定义事件
    },
  },
};
</script>

三、vue父组件调用子组件方法或属性

在Vue中,父组件可以通过ref引用子组件,并通过该引用调用子组件的方法或访问子组件的属性。

<!-- 父组件 -->
<template>
  <div>
    <h2>父组件</h2>
    <ChildComponent ref="childComponentRef" />
    <button @click="callChildMethod">调用子组件方法</button>
    <p>{{ childComponentMessage }}</p>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
  data() {
    return {
      childComponentMessage: '',
    };
  },
  methods: {
    callChildMethod() {
      this.$refs.childComponentRef.childMethod(); // 调用子组件的方法
      this.childComponentMessage = this.$refs.childComponentRef.childProperty; // 访问子组件的属性
    },
  },
};
</script>
<!-- 子组件 ChildComponent.vue -->
<template>
  <div>
    <h3>子组件</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      childProperty: 'Message from child component',
    };
  },
  methods: {
    childMethod() {
      console.log('Method called from parent component');
    },
  },
};
</script>

四、vue子组件修改prop值

1、通过父传子,子调用父方法传参数实现

<!-- 父组件 -->
<template>
 	<child-view :num="num" @updateNum="updateNum"></child-view>
</template>
<script>
 import childView from './assembly/child'
 export default {
    components: {childView},
    data() {
      return {
        num: 2
		}
	},
	methods: {
      updateNum(num){
        this.num = num
      }
 }
 </script>
<!-- 子组件 -->
<<template>
  <div>
    <p>父传过来的值:{{num}}</p>
    <button @click="changeNum">加一</button>
  </div>
</template>

<script>
  export default {
    name: 'child',
    props:{
      num: {
        type:Number,
        default: 0
      }
    },
    methods:{
      changeNum(){
        this.$emit("updateNum",this.num + 1)
      }
    }
  }

2、通过.sync修饰符以及$emit配合update:实现,该方法只有vue2可以使用

<!-- 父组件 -->
<template>
 	<child-view :num.sync="num"></child-view>
</template>
<script>
 import childView from './assembly/child'
 export default {
    components: {childView},
    data() {
      return {
        num: 2
		}
	}
 }
 </script>
<template>
  <div>
    <p>父传过来的值:{{num}}</p>
    <button @click="changeNum">加一</button>
  </div>
</template>

<script>
  export default {
    name: 'child',
    props:{
      num: {
        type:Number,
        default: 0
      }
    },
    methods:{
      changeNum(){
        this.$emit("update:num",this.num + 1)
      }
    }
  }
</script>

3、使用v-modal绑定一个数据源

<!-- 父组件 -->
<template>
 	<child-view v-modal="num"></child-view>
</template>
<script>
 import childView from './assembly/child'
 export default {
    components: {childView},
    data() {
      return {
        num: 2
		}
	}
 }
 </script>
<template>
  <div>
    <p>父传过来的值:{{num}}</p>
    <input type="text" :value="value" @input="$emit('input',$event.target.value)">
  </div>
</template>

<script>
  export default {
    name: 'child',
    props:["value"]
  }
</script>
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

smileAgain-lg

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

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

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

打赏作者

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

抵扣说明:

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

余额充值