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

news/2024/7/10 3:17:14 标签: vue, js

Vue项目中如何在子组件中直接调用父组件的方法:

方法一: 直接在子组件中通过this.$parent.event来调用父组件中的方法

//父组件
<template>
  <div>
    <child></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>
//子组件
<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$parent.fatherMethod();
      }
    }
  };
</script>

方法二:在子组件里用$emit向父组件触发一个事件,父组件监听这个事件

//父组件
<template>
  <div>
    <child @fatherMethod="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

//子组件
<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$emit('fatherMethod');
      }
    }
  };
</script>

方法三: 父组件把方法传入子组件中,在子组件里直接调用这个方法

//父组件
<template>
  <div>
    <child :fatherMethod="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

//子组件
<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
        if (this.fatherMethod) {
          this.fatherMethod();
        }
      }
    }
  };
</script>

http://www.niftyadmin.cn/n/1770323.html

相关文章

vue 生成二维码

一、页面中生成二维码方法 ①、首先安装qrcodejs2插件 npm install qrcodejs2 --save注意&#xff1a;安装完成之后可以在main.js中全局配置&#xff0c;也可以在单个页面中去引用 ②、引入 1&#xff09;单页面中引入 import QRCode from qrcodejs2;2&#xff09;全局配置…

elementUI中 form表单自定义验证方法

一、在form表单中经常去验证一些内容&#xff0c;有的是只比较简单的一些提示&#xff0c;有的则是需要自定义&#xff0c;实现方法如下&#xff1a; <template> <el-form :model"ruleForm2" :rules"dataRule" ref"dataForm" keyup.en…

elementUi 按钮点击之后取消状态

elementUi 中的按钮&#xff0c;点击操作之后按钮的状态会一直存在&#xff0c;如果想要取消的话就手动触发一下&#xff0c;如下&#xff1a; let target evt.target; if(target.nodeName "SPAN"){target evt.target.parentNode;}target.blur();点击的时候去执行…

ElementUI Message提醒框点击多次只显示一个

点击按钮message提示&#xff0c;点击多次就会出现多个&#xff0c;这个时候需要手动去关闭一下message所有的实例&#xff0c;如下&#xff1a; this.$message.closeAll(); //手动关闭所有实例 this.$message({message: 您还没有选中哦~,type: warning });

vue 项目build后背景图路径不对的问题

1、用css写背景图样式是以相对路径来写的 .details-share{position: fixed;top: 0;left: 0;width: 100%;height: 114px;background: url("../assets/images/share.png");background-size: 100%; }当使用npm run dev命令本地访问的时候&#xff0c;背景图片是正常显示…

uni-app 封装uni.request

1、首先建立一个文件夹&#xff0c;然后新建一个api.js&#xff0c;js中这样来定义&#xff0c;如下&#xff1a; //const baseUrl http://XXXX.com const request (url , date {}, type GET, header { }) > {return new Promise((resolve, reject) > {uni.req…

Ajax之同步请求和异步请求的区别?使用场景?

一、区别 ①、异步&#xff1a; 在异步模式下&#xff0c;当我们使用AJAX发送完请求后&#xff0c;可能还有代码需要执行。这个时候可能由于种种原因导致服务器还没有响应我们的请求&#xff0c;但是因为我们采用了异步执行方式&#xff0c;所有包含AJAX请求代码的函数中的剩余…

uni-app 中将改成异步请求同步化操作

在uni-app中&#xff0c;uni.request等许多接口都是异步的&#xff0c;有时候需要等接口返回数据后在执行下一步的操作&#xff0c;这个时候我们就用到了异步请求同步化操作。 解决方法&#xff1a; 总体思路就是使用async await&#xff0c;使异步问题同步化。需要 注意 的…