vue3 父组件调用子组件方法

  1. 使用ref属性(推荐方式)
    • 步骤一:在父组件中给子组件添加ref属性
      • 在父组件的模板中,给子组件标签添加ref属性,用于获取子组件的引用。例如,有一个父组件Parent.vue和子组件Child.vue,在Parent.vue中:
      <template>
        <div>
          <h1>父组件</h1>
          <Child ref="childComponentRef"></Child>
          <button @click="callChildMethod">调用子组件方法</button>
        </div>
      </template>
      <script setup>
      import { ref } from 'vue';
      import Child from './Child.vue';
      const childComponentRef = ref(null);
      const callChildMethod = () => {
        if (childComponentRef.value) {
          // 假设子组件有一个名为childMethod的方法
          childComponentRef.value.childMethod();
        }
      };
      </script>
      
    • 步骤二:在子组件中暴露方法(使用defineExpose
      • 在子组件Child.vue中,通过defineExpose函数将希望被父组件调用的方法暴露出来。
      <template>
        <div>
          <h2>子组件</h2>
        </div>
      </template>
      <script setup>
      const childMethod = () => {
        console.log('子组件的方法被调用了');
      };
      // 将方法暴露给父组件
      defineExpose({
        childMethod
      });
      </script>
      
  2. 通过$parent访问(不推荐,耦合性强)
    • 这种方式是通过$parent属性来访问父组件。在子组件中,$parent指向父组件实例。但是这种方式会使组件之间的耦合性变强,不符合组件化的设计理念。
    • 示例:假设父组件中有一个方法parentMethod,子组件想要调用它
      • 父组件(Parent.vue
        <template>
          <div>
            <h1>父组件</h1>
            <Child></Child>
          </div>
        </template>
        <script setup>
        import Child from './Child.vue';
        const parentMethod = () => {
          console.log('父组件的方法被调用了');
        };
        </script>
        
      • 子组件(Child.vue
        <template>
          <div>
            <h2>子组件</h2>
            <button @click="callParentMethod">调用父组件方法</button>
          </div>
        </template>
        <script setup>
        const callParentMethod = () => {
          // 通过$parent访问父组件方法
          if (this.$parent && typeof this.$parent.parentMethod === 'function') {
            this.$parent.parentMethod();
          }
        };
        </script>
        
    • 不过在Vue 3中,$parent的使用可能会受到一些限制,并且这种方式破坏了组件的封装性,不建议在实际开发中大量使用。通常情况下,优先考虑使用ref来获取子组件引用并调用其方法。
### Vue3子组件暴露方法父组件Vue3 中,`defineExpose` 宏函数用于有选择地暴露子组件中的属性或方法父组件。通过这种方式,父组件可以直接调用这些公开的方法。 当定义 `setup()` 函数返回的对象时,默认情况下不会自动将其内容暴露给外部[^1]。为了使某些特定的属性或方法可被外界访问,需显式声明要暴露的内容: ```javascript import { defineComponent } from &#39;vue&#39;; export default defineComponent({ setup() { const validate = () => console.log(&#39;Validation logic here&#39;); // 只暴露出希望父组件能够使用的部分 defineExpose({ validate }); return {}; } }); ``` 上述代码片段展示了如何利用 `defineExpose` 来指定哪些成员应该对外可见。这里只选择了 `validate` 方法作为示例来说明这一点。 对于父组件而言,在模板中使用 `<component ref="childRef"></component>` 的形式挂载子组件,并设置好对应的 `ref` 属性以便后续操作该实例。之后可以通过这个引用获取并执行已暴露出来的任何公共接口: ```html <template> <div class="parent"> <!-- 假设这是子组件 --> <ChildComponent ref="myChild"/> <button @click="callValidate">Call Validate</button> </div> </template> <script> import ChildComponent from &#39;./components/ChildComponent.vue&#39;; import { ref, onMounted } from &#39;vue&#39;; export default { components: { ChildComponent }, setup(){ let myChild = ref(null); function callValidate(){ if(myChild.value){ myChild.value.validate(); // 调用子组件内的validate方法 } } return{ myChild, callValidate }; } }; </script> ``` 这段脚本显示了怎样创建一个按钮触发器去间接激活嵌套在其下的子组件里的功能逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值