首页 > 其他分享 >vue父子组件

vue父子组件

时间:2023-01-11 15:12:45浏览次数:40  
标签:vue console parent refs 父子 获取 组件 方法

父组件主动获取子组件的数据和方法

1..调用子组件的时候 定义一个ref

<headerchild ref="headerChild"></headerchild>

2.在父组件里面通过

this.$refs.headerChild.属性
this.$refs.headerChild.方法

子组件主动获取父组件的数据和方法

在子组件里面通过

this.$parent.属性
this.$parent.方法

演示代码:

//父组件
<template>
  <div id="header">  
    <headerchild ref="headerChild"></headerchild>
    <button @click="getChild()">父组件获取子组件的数据和方法</button>
  </div>
</template>
<script>
import HeaderChild from './HeaderChild'
export default {
  data () {
      return {
          title:'我是父组件的数据'
      }
  },
  methods: {
     getChild (){
         console.log(this.$refs.headerChild.name)
     },
     run (){
         console.log("我是父组件里面的方法")
     }
  },
  components: {
      'headerchild': HeaderChild
  }
}
</script>
<style lang="sass" scoped>

</style>

//子组件
<template>
  <div id="headerchild">
      <button @click="getParent()">获取父组件的数据和方法</button>
  </div>
</template>
<script>
export default {
  data () {
      return {
          name:'我是子组件里面的数据'
      }
  },
  methods:{
      getParent(){
          console.log(this.$parent.title) /*获取整个父组件*/
          this.$parent.run()/*获取父组件的方法*/
      }
  },
  props:['title','run','home'] /*通过props接收父组件传递过来的数据 */
}
</script>

链接:https://www.jianshu.com/p/b6c553b89f26

标签:vue,console,parent,refs,父子,获取,组件,方法
From: https://www.cnblogs.com/axin85/p/17043807.html

相关文章

  • Vue Review
    Vuereview参考网址https://www.cnblogs.com/songhaixing/p/14706979.html三种获取data里面的值的方法......<body><divid="container">{{msg}}......
  • 第三节:内置组件、扩展库uni-ui组件简介、导入方式、修改主题风格
    一. 内置组件       二. 扩展库uni-ui简介       三.uni-ui的导入方式         四.uni-ui修改主题风格   ......
  • vue2 项目引入Fontawesome
    官网:https://fontawesome.com/1.安装```powershellnpmi--save@fortawesome/fontawesome-svg-coreUsingVue2.x```powershell$npmi--save@fortawesome/vue......
  • vue3 如何在 jsx中使用 component 组件
    component组件不像其它的内置组件(tansition、transitionGroup),可以直接从vue中直接导出,所有要在jsx使用component就要使用h函数使用vue内置组件//xxx.jsxim......
  • Vue2.002.开发过程中部分功能实现
    01.Vue中动态加载图片失败时,默认图片的配置>> 引入图片:  importdefaultImgfrom'@/assets/default.png'>> img 标签配置@error 事件,   @error="......
  • vue指令
    指令(1)表单绑定    v-model:双向绑定     多个checkbox,v-model="数组",:value=""     单个checkbox,v-model="布尔值"     应用:......
  • vue中实现图片拖拽
    <!--拖拽排序--><!--给外层容器添加vuedraggable这个标签--><vuedraggableclass="draggable-container"v-model="pram.i......
  • 使用mui+vue框架做原生app的坑(二) 打包
    怎么打包,这里不赘述,网上有很多教程。  打包成功后,app会出现未加入push功能。    后面查询需要这样配置权限,权限要在manifest......
  • 如何发布组件模板?
    前置准备:完成设计制作的组件模板具体步骤:提交模板(编辑器内提交后可以在本账号下进行复用)补充模板信息提交审核进行上架步骤分解:编辑器发布点击提交模板按钮选择要发布......
  • 在vue中使用CSS变量
    首先,我们要先知道什么是CSS变量,可以先看这篇文章在我们知道什么是CSS变量之后,我们尝试把它在项目中运用起来,一些需要动态计算的值,我们就可以使用它快速的实现效果。以下为......