Vue父组件调用子组件的方法

vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如:

父组件:

<template>
  <div @click="fatherMethod">
    <child ref="child"></child>
  </div>
</template>
<script>
  import child from ‘~/components/dam/child.vue‘;
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {this.$refs.child.childMethods();
      }
    }
  };
</script>

子组件:

<template>
  <div>{{name}}</div>
</template>
<script>
  export default {
    data() {
      return {
        name: ‘测试‘
      };
    },
    methods: {
      childMethods() {
        console.log(this.name);
      }
    }
  };
</script>

在父组件中, this.$refs.child 返回的是一个vue实例,可以直接调用这个实例的方法,嗯,就酱~

原文地址:https://www.cnblogs.com/jin-zhe/p/9523029.html

时间: 2024-08-01 16:13:44

Vue父组件调用子组件的方法的相关文章

angular 中父组件调用子组件的方法

angular中 父组件调用子组件的方法 -- 使用 @ViewChild 装饰器修饰子组件,获取方法,调用 除此之外 ViewChild 还可以获取 DOM ,操作 DOM , 详见: https://www.cnblogs.com/monkey-K/p/11567098.html 1. html 中使用 #var 方式标记 子组件 <div style="border: 1px solid red"> <p>子组件:</p>> <ap

VUE 父组件调用子组件弹窗

想搞一个新增编辑弹窗,和列表页面分开 先来一个父组件调用子组件弹窗的demo 父组件 <template> <div> <el-button @click="show">按钮</el-button> <!-- 新增编辑弹框子组件 --> <add-or-update :addOrUpdateVisible="addOrUpdateVisible" @changeShow="showAddOr

vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><div id="app"></div></body><script src="node_modules/vue/

父组件调用子组件中的方法- this.$refs.xxx.子组件方法();

子组件中有一个说的方法 在父组件中去调用当你点击的时候 去调用子组件中的方法 fu.vue 在父组件的方法中调用子组件的方法,很重要 this.$refs.mychild.parentHandleclick(); { <template> <div> <button @click="clickParent">点击 调用子组件</button> <child ref="mychild"></child&

vue中父组件调用子组件函数

用法: 子组件上定义ref="refName",  父组件的方法中用 this.$refs.refName.method 去调用子组件方法 详解: 父组件里面调用子组件的函数,父组件先把函数/方法以属性形式传给子组件:那么就需要先找到子组件对象 ,即  this.$refs.refName. 然后再进行调用,也就是 this.$refs.refName.method 如下: 子组件: <template> <div> childComponent </di

父组件调用子组件的方法

我们都知道通过$ref可以获取到某个DOM,但是它也可以用来获取子组件的实例,调用子组件的方法 父组件 子组件 原文地址:https://www.cnblogs.com/xianhao/p/10475314.html

React父组件调用子组件

通点击父组件按钮调用子组件的方法 父组件: import React, {Component} from 'react'; import ChildComponent from './child'; export default class ParentComponent extends Component { render() { return( <div> <ChildComponent onRefChild={this.onRefChild} /> <button on

父页面调用子页面的方法

其实一句话就可以调用子页面的方法了,何必整得那么纠结是吧,感谢java大神: var dsjg1=document.getElementById("div-dsjg").contentWindow.checkEmpty(); 我的子页面有一个方法: 子页面js: function checkEmpty(){ alert("我是儿子!"); } 子页面body内容省略. 父页面: <iframe id="div-dsjg" src="

layer父界面调用子弹窗的方法和获取子弹窗的元素值总结

1 layer.open({ 2 type: 2 3 ,title: false //不显示标题栏 4 ,closeBtn: false 5 ,area: ['460px', '45%'] 6 ,shade: 0.5 7 ,id: 'LAY_layuipro·' //设定一个id,防止重复弹出 8 ,btn: ['确定','取消'] 9 ,btn1: function(index, layero){ 10 11 var body = layer.getChildFrame('body', ind