您的当前位置:首页正文

Vue 调用子组件方法

来源:要发发知识网
<div id="app">
  <child ref="child"></child>
  <button @click="callChildMethod">
    callChildMethod
  </button>
</div>

<script 
<script type="application/javascript">
 {
  template: '<div></div>',
  methods: {
    // 定义方法用于父实例调用
    childMethod() {
      console.log('call child method success!');
    }
  }
});

new Vue({
  el: '#app',
  methods: {
    callChildMethod() {
      console.log('call app method success!');
      // 通过 refs 找到子组件后直接调用方法
      this.$refs.child.childMethod();
    }
  }
})
</script>

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文