vue父组件调用子组件中的方法、值的几种方式

1. ref

直接在父组件内部给子组件标签添加ref属性,然后通过ref属性来调用子组件的方法。

父组件
// Parent.vue

<template>
  <div>
    <button @click="handleClick">按钮</button>
    <Child ref="child"></Child>
  </div>
</template>

<script>
import Child from './components/child.vue'
export default {
  name: "Father",
  components: {
    Child,
  },
  methods: {
    handleClick() {
      this.$refs.child.sing()
    },
  },
}
</script>

<style scoped lang="scss">
</style>
子组件
// Child.vue

<template>
  <div>
    <p>我是子组件</p>
  </div>
</template><script>
export default {
  name: "Child",
  methods: {
    sing() {
      console.log('使用refs直接调用子组件方法')
    },
  },
}
</script>

<style scoped lang="scss">
</style>

2. $children

在父组件的方法中,通过 $children 属性来访问子组件实例,并调用子组件的方法,返回的是一个存放所有子组件的数组。

父组件
// Parent.vue

<template>
  <div>
    <button @click="handleClick">点击我调用子组件方法</button>
    <Child ref="child"></Child>
  </div>
</template>

<script>
export default {
  methods: {
    callChildMethod() {
      const childComponent = this.$children[0];
      childComponent.childMethod();
    }
  }
}
</script>

<style scoped lang="scss">
</style>
子组件
// Child.vue

<template>
  <div>
    <p>我是子组件</p>
  </div>
</template><script>
export default {
  name: "Child",
  methods: {
    childMethod() {
      console.log('使用$children直接调用子组件方法')
    },
  },
}
</script>

<style scoped lang="scss">
</style>

3. $emit $on

父组件
// Parent.vue

<template>
  <div>
    <button @click="handleClick">点击我调用子组件方法</button>
    <Child ref="child"></Child>
  </div>
</template><script>
import Child from './components/child.vue'
export default {
  components: {
    Child,
  },
  methods: {
    handleClick() {
      this.$refs.child.$emit("sing")	//子组件里$on中的名字
    },
  },
}
</script>

<style scoped lang="scss">
</style>
子组件
// Child.vue

<template>
  <div>
    <p>我是子组件</p>
  </div>
</template><script>
export default {
  mounted() {
    this.$nextTick(function () {
      this.$on('sing', function () {
        console.log('$emit配合$on实现调用子组件方法')
      })
    })
  },
}
</script>

<style scoped lang="scss">
</style>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue组件调用组件方法有以下几种方式: 1. 使用`ref`引用组件:在组件使用`ref`给组件命名,然后通过`$refs`访问组件的实例,从而调用组件方法。 ```html <template> <div> <child-component ref="child"></child-component> <button @click="callChildMethod">调用组件方法</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { callChildMethod() { this.$refs.child.childMethod(); } } } </script> ``` 2. 使用事件派发($emit):在组件定义一个方法,然后通过`this.$emit`触发自定义事件,组件监听该事件并调用相应的方法。 ```html <template> <div> <button @click="childMethod">点击触发自定义事件</button> </div> </template> <script> export default { methods: { childMethod() { this.$emit('child-event'); } } } </script> <template> <div> <child-component @child-event="parentMethod"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { parentMethod() { // 调用组件方法 } } } </script> ``` 3. 使用`$children`:在组件使用`$children`获取所有组件的实例数组,然后调用相应的方法。 ```html <template> <div> <child-component></child-component> <button @click="callChildMethod">调用组件方法</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { callChildMethod() { this.$children[0].childMethod(); } } } </script> ``` 这些是Vue组件调用组件方法几种常见方式,你可以根据实际情况选择适合你的方式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值