首页 > 其他分享 >vue子组件获取父组件数据

vue子组件获取父组件数据

时间:2023-09-10 10:31:54浏览次数:32  
标签:vue methods default father 获取 export child 组件

1、使用this.$parent.event直接调用
//父组件
<template>
  <div>
    <child></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      father() {
        console.log('提交');
      }
    }
  };
</script>

//子组件
<template>
  <div>
    <button @click="submit()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      submit() {
        this.$parent.father();
      }
    }
  };
</script>
2、通过$emit触发一个事件,父组件监听
//父组件
<template>
  <div>
    <child @father="father"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      father() {
        console.log('提交');
      }
    }
  };
</script>

//子组件
<template>
  <div>
    <button @click="submit()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      submit() {
        this.$emit('father');
      }
    }
  };
</script>
3、使用props
//父组件
<template>
  <div>
    <child :father="father"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      father() {
        console.log('提交');
      }
    }
  };
</script>

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

三种区别: 三种都可以实现子组件调用父组件的方法,但是效率有所不同


this.$parent.event可以调用父组件身上的方法,无需绑定在子组件身上。缺点:有时候会失效,暂未发现触发点,不建议使用。

$emit可以调用父组件在子组件身上自定义的事件,需要用@前缀。建议使用此种方式

props可以调用父组件绑定在子组件身上的事件,需要:前缀。在router-view身上使用失效

标签:vue,methods,default,father,获取,export,child,组件
From: https://blog.51cto.com/u_14914383/7423817

相关文章

  • 文字组件表格中的文字纵向居中问题
    问题:文字组件中插入的表格,其中文本内容纵向如何设置居中解决:选取表格》表格工具》对齐方向》垂直居中 如果设置了垂直居中后文字仍是靠下,就需要考虑是否存在空段落, 以下设置可以显示编辑标记:开始》段落》显示/隐藏段落标记如果确实是空段落造成的,可以使用替换解决:选取表......
  • vue3 如何获取格式为 proxy 值的原始值
    vue3中变量多使用ref,reactive来声明。如下:constfirstName=ref('Tom')constlastName=ref('Jerry')constform=reactive({name:'',sex:'',age:'',area:'',loves:[]})当想使用上面的数据时,如下://......
  • export ‘Vue‘ (imported as ‘Vue‘) was not found in ‘vue‘
    export'Vue'(importedas'Vue')wasnotfoundin'vue'(possibleexports:$computed,$fromRefs,$raw,$ref,BaseTransition,Comment,EffectScope,Fragment,KeepAlive,ReactiveEffect,Static,Suspense,Teleport,Text,Transit......
  • Salesforce Marketing Cloud 获取Token
    当我们要调用MarketingCloud的Api时,不管是SOAP还是REST都需要进行验权Authorization。如果我们需要使用v2版本去获取token,我们需要传递5个参数,其中有3个参数是必须要传递的,2个可选参数,参考官网的文档AccessTokenforServer-to-ServerIntegrations|MarketingCloudAPIsand......
  • Vue进阶(幺陆陆):组件实例 $el 详解
    this指向组件实例,$el用于获取Vue实例挂载的DOM元素,在mounted生命周期中才有效,之前的钩子函数内无效。如下代码所示,Vue脚手架中,$el指向当前组件template模板中的根标签。<template><divid="root">Lorem,ipsum</div></template><script>exportdefault......
  • 从零开始使用vue2+element搭建后台管理系统(前期准备)
    准备开始1.安装node(node-v查询版本号)(下载地址:https://nodejs.org/en/download/)2. 安装淘宝镜像 npminstall-gcnpm--registry=https://registry.npm.taobao.org3.安装webpack,以全局的方式安装 npminstallwebpack-g4.全局安装vue以及脚手架vue-cli npminst......
  • 四、若依获取动态路由
    1、获取动态路由2、全局路由守卫2.1)用户在登录后,会发出3个请求,分别是login,getinfo,getRouters。分别代表了请求登录,获取用户角色和权限,获取动态路由。getinfo,getRouters是配置在路由守卫中的。3、前端3.1)在router在index.js文件中,默认已经读取了公共(静态)路由数据,系统确实......
  • 一套基于spring boot vue开发的UWB定位系统源码 UWB全套源码
    现代制造业厂区面积大、人员数量多、物资设备不断增加,随着工业信息化技术的发展,大型制造企业中对人员、车辆、物资的管理要求越来越细致。高精度定位管理系统使用UWB室内定位技术,通过在厂区安装定位基站,为人员或设备佩戴定位标签的形式,实现人员精准实时定位。可以实现人员、车辆物......
  • Python获取主目录的方法
    用os.path.expanduser模块获取主目录为了获得用户的主目录,我们可以使用Python中的os.path.expanduser。我们必须在os.path.expanduser()函数里面传递一个字符串字符~,它将返回当前登录用户的主目录路径。它使用内置的密码数据库或pwd模块来搜索主目录的路径,pwd模块提供用户......
  • 利用类加载器获取项目中的资源文件
    当需要获取项目中的配置文件时,我们可以使用类加载器来获取,主要利用的就是getResource和getResourceAsStream方法一、获取项目的classpath路径以一个springboot项目举例,在idea中运行时,classpath路径指的就是idea运行时给我们创建的target/classes目录,我们先尝试在idea中运......