vue子组件怎么调用父组件的方法
方法:1、子组件中通过“this.$parent.event”来调用父组件的方法。2、子组件用“$emit”向父组件触发一个事件,父组件监听这个事件即可。3、父组件把方法传入子组件中,在子组件里直接调用这个方法即可。
本教程操作环境:windows7系统、vue2.9.6版,DELL G3电脑。
Vue中子组件调用父组件的方法,这里有三种方法提供参考
第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法
父组件
<template> <p> <child></child> </p> </template> <script> import child from '~/components/dam/child'; export default { components: { child }, methods: { fatherMethod() { console.log('测试'); } } }; </script>
子组件
<template> <p> <button @click="childMethod()">点击</button> </p> </template> <script> export default { methods: { childMethod() { this.$parent.fatherMethod(); } } }; </script>
第二种方法是在子组件里用$emit
向父组件触发一个事件,父组件监听这个事件就行了。
父组件
<template> <p> <child @fatherMethod="fatherMethod"></child> </p> </template> <script> import child from '~/components/dam/child'; export default { components: { child }, methods: { fatherMethod() { console.log('测试'); } } }; </script>
子组件
<template> <p> <button @click="childMethod()">点击</button> </p> </template> <script> export default { methods: { childMethod() { this.$emit('fatherMethod'); } } }; </script>
第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法
父组件
<template> <p> <child :fatherMethod="fatherMethod"></child> </p> </template> <script> import child from '~/components/dam/child'; export default { components: { child }, methods: { fatherMethod() { console.log('测试'); } } }; </script>
子组件
<template> <p> <button @click="childMethod()">点击</button> </p> </template> <script> export default { props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod() { if (this.fatherMethod) { this.fatherMethod(); } } } }; </script>
【相关推荐:vue.js教程】
以上是vue子组件怎么调用父组件的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何在Vue中实现单表头和多表身的电子化报价表单在现代化的企业管理中,报价表单的电子化处理是提高效率和...

为什么vue-router跳转后控制台网络中没有页面请求信息?在使用vue-router进行页面跳转时,你可能会注意到一个现�...

在前端如何实现不同品牌高拍仪的拍照上传功能在开发前端项目时,常常会遇到需要集成硬件设备的需求。对于...

关于VueMaterialYear...

Vue2中实现el-table表格分组拖拽排序在Vue2中使用el-table表格实现分组拖拽排序是一个常见的需求。假设我们有一个...
