Vue2中子组件调用父组件的方法,父组件调用子组件的方法,父子组件互相传值和方法调用

简介: Vue2中子组件调用父组件的方法,父组件调用子组件的方法,父子组件互相传值和方法调用

💟 上一篇文章 组件之间的多种通信方式,一文彻底搞懂组件通信!

📝 系列专栏 vue从基础到起飞


一、父组件调用子组件的方法

场景:父组件调用子组件的方法,父组件取值子组件的属性参数

父组件(包含的子组件引用标签中加上ref属性),这时给子组件标签使用ref,引用指向的就是子组件的实例

父组件:

<template>
  <div id="app">
    <test ref="test"/>
    <button @click="gettest">获取test组件中的值</button>
  </div>
</template>
 
<script>
import test from "./components/test.vue";
 
export default {
  components: {
    test
  },
  data() {
    return {}
  },
  methods: {
    gettest() {
      console.log(this.$refs.test.message)
    }
  }
};
</script>

子组件:

<template>
  <div>
      {{ message }}
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      message: "hello world"
    }
  }
}
</script>

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

场景:自定义一个通用组件,需要调用父组件的方法进行计算

1、使用this.$emit()向父组件触发一个事件,父组件监听这个事件即可

父组件:在父组件中给子组件上添加一个自定义函数,把父组件中的方法传递进去

<template>
  <div>
    <child @method="method"></child>
  </div>
</template>
<script>
  import child from './components/childCompoent'
  export default {
    components: {
      child
    },
    methods: {
      method(data) {
        console.log(data+'调用父组件方法');
      }
    }
  };
</script>

子组件:子组件中通过this.$emit("父组件传递过来的函数","子组件数据")来触发父组件函数

<template>
  <div>
    <button @click="method()">点击调用父组件方法</button>
  </div>
</template>
<script>
  export default {
    methods: {
      method() {
        this.$emit('method','子组件');
        //this.$emit('method');没有参数的话,直接调用即可
      }
    }
  };
</script>

2、直接在子组件中通过“this.$parent.event”来调用父组件的方法

父组件:

<template>
  <div>
    <child></child>
  </div>
</template>
<script>
  import child from './components/childCompoent';
  export default {
    components: {
      child
    },
    methods: {
      method() {
        console.log('父组件方法');
      }
    }
  };
</script>

子组件:

<template>
  <div>
    <button @click="childMethod()">点击按钮</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$parent.method();
      }
    }
  };
</script>

3、父组件把方法传入子组件中,在子组件里直接调用

父组件:

<template>
  <div>
    <child :method="method"></child>
  </div>
</template>
<script>
  import child from './components/childCompoent';
  export default {
    components: {
      child
    },
    methods: {
      method() {
        console.log('父组件方法');
      }
    }
  };
</script>

子组件:

<template>
  <div>
    <button @click="childMethod()">点击按钮</button>
  </div>
</template>
<script>
  export default {
    props: {
      method: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
          this.method();
        }
      }
    }
  };
</script>

相关文章
|
数据格式
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
本文介绍了在ElementUI和ElementPlus中使用`el-table`组件实现合并行功能的技巧,包括多列合并和单列合并的方法,并提供了相应的示例代码和运行效果。
10607 46
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
|
JavaScript
Vue2之父与子组件互相传参和方法调用
这篇文章介绍了Vue 2中父子组件之间互相传参和方法调用的四种方式:通过`ref`调用子组件方法、通过`props`调用父组件方法、父组件通过`:param`传参给子组件,以及子组件通过`emit`传参给父组件。
2827 0
|
JavaScript
Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName、window.function)
Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName、window.function)
Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName、window.function)
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
|
JavaScript API 索引
js中的reduce()方法 讲解 和实现
`reduce()` 方法对数组元素依次应用一个回调函数,将结果累计并最终返回单一值。语法为 `reduce(callback(accumulator, currentValue, currentIndex, array), initialValue)`。参数包括累计器(初次为初始值或首元素)、当前元素值、索引及数组自身。此方法需返回值供下一轮迭代使用。常见应用场景包括计算数组总和与平均值、统计元素频率、过滤与转换数组内容及去除重复项等。例如,可通过 `reduce()` 快速计算 `[1, 2, 3, 4, 5]` 的总和或对对象属性值求和。此外,还可自定义实现 `reduce()` 方法
7375 1
|
JavaScript
Vue.js 中父组件调用子组件的方法
Vue.js 中父组件调用子组件的方法
1290 2
|
JavaScript 网络架构
vue 使用 this.$router.push 传参数,接参数的 query或params 两种方法示例
vue 使用 this.$router.push 传参数,接参数的 query或params 两种方法示例
3111 4
|
JavaScript API
vue3父子组件相互调用方法详解
vue3父子组件相互调用方法详解
|
JavaScript
vue 【详解】父子组件传值、父子组件数据双向绑定 —— : | update: |.sync | v-bind.sync | v-model(含model选项和自定义v-model)
vue 【详解】父子组件传值、父子组件数据双向绑定 —— : | update: |.sync | v-bind.sync | v-model(含model选项和自定义v-model)
726 1