子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp
(一) popsDowm
三种方法获取父组件数据:被动获得(1);主动获取(2)。
1.被动获得:
父组件:v-bind: 绑定变量参数和方法参数;子组件:props 接收参数。可以在模板中直接使用也可以 this.参数 获得
|
v-bind:name="yourName"
|
props:['name'] |
template: {{name}} js: this.name |
|
v-bind:fatherMeth="regMeth"
|
props:{fatherMeth: Function} | js: this.fatherMeth() |
2.主动获取:
①this.$parent.变量和方法
this.$parent.yourName;this.$parent.fatherTest().
如果获取不到,在父组件将 this 传递给子组件::father="this"
this.father.youerName;this.father.fatherTest().
② 发送 emit 事件
this.$emit('fatherOnEmite',this.childVariate)
(二) eventUp:
两种方法: 在父组件设置 ref 属性;监听 emit (订阅)。
父组件:ref="child1"
this.$refs.child1.desc;this.$refs.child1.childTest()
父组件:v-on:fatherOnEmite="onEmite"
注意:由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。updated 不能保证全部组件都加载用 this.nextTick
代码github:https://github.com/dopocheng/alone-part
父组件
<template>
<div>
propsParent!!!<br /><br />
<!-- prop 静态赋值 -->
<!-- prop 变量动态赋值 yourName、obj-->
<Compon
title="my journey with vue"
v-bind:name="yourName"
v-bind:student="obj"
v-bind:fatherMeth="regMeth"
:father="this"
v-on:fatherOnEmite="onEmite"
ref="child1"
/><br />
<p @click="updated_data_counter">{{updated_data}} 点击 使用 this.$refs 获取子组件数据</p>
</div>
</template> <script>
import Compon from '../../complex-component/components'
export default {
components: { Compon },
data() {
return {
yourName: "dpc",
obj: {
id: "007AB",
age: 18
},
updated_data: 0
}
},
created() {
},
updated() {// 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。updated 不能保证全部组件都加载用 this.nextTick
this.fatherCall()
},
methods: {
fatherTest() {
console.log("父组件方法")
},
fatherCall() {
// 因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!$refs 也不是响应式的,因此你不应该试图用它在模板中做数据绑定
// 1.等待页面渲染完成后再去获取子组件变量和方法
console.log("***********父组件主动获取***********")
this.$nextTick(() => {console.log("this.$nextTick 最后调用",this.$refs.child1.desc)})
this.$nextTick(() => {console.log("this.$nextTick 最后调用"); this.$refs.child1.childTest()})// console.log 无法打印方法
console.log("***********父组件主动获取 end***********")
},
onEmite(arg) {
// 2. 获取子组件数据
console.log("父组件注册(订阅) v-on 方法", arg);
},
regMeth() {
console.log("父组件方法直接传入组件");
},
updated_data_counter() {
this.updated_data++
}
}
}
</script>
子组件:
<template>
<div>
complex-component components!!!<br /><br />
<p>{{title}}</p>
<p>{{name}}</p>
<p>{{student}}</p>
</div>
</template>
<script>
export default {
// props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
// 注意那些 prop 会在一个组件实例创建之前进行验证,所以实例的属性 (如 data、computed 等) 在 default 或 validator 函数中是不可用的。
props:{// 一: 被动获得父组件的参数 (v-bind 参数)
title: {type:String, default:"title", required: true},
name: {
type: String,
validator: function (value) {
// 这个值必须匹配下列字符串中的一个,否则控制台有警告
return ['dpc', 'warning', 'danger'].indexOf(value) !== -1
}
},
student: {type:Object,
default: function () {
return { message: 'not student' }
}},
fatherMeth: Function,
father: Object },
data() {
return {
childVariate: "在子组件调用父组件的oo!!!" ,
desc: "父组件调用子组件属性!!!",
}
},
created() {
this.passiveGain()
this.activeAcquirement()
},
methods: {
childTest() {
console.log("父组件调用子组件方法!!!")
},
passiveGain() {
console.log("***********child passive gain***********")
//一:被动获得父组件值(通过 v-bind:参数)和方法
console.log(this.title);
this.fatherMeth()
console.log("***********child passive gain end***********")
},
activeAcquirement() {
console.log("***********child active acquirement***********")
// 二.主动获取父组件变量值或方法
console.log("主动获取父组件变量值或方法",this.$parent.yourName)
this.$parent.fatherTest()
// 1.如果获取不到,就把父组件对象 this 传递给子组件,在子组件接收下 this.father (定义的参数).变量
console.log("父组件对象 this 传递给子组件",this.father.yourName)
this.father.fatherTest() // 2.子组件使用 $emit 触发父组件事件
this.$emit('fatherOnEmite',this.childVariate)
console.log("***********child active acquirement end***********")
}
}
}
</script>
子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp的更多相关文章
- 微信小程序将外部数据从父组件中传入到子组件
小程序组件开发遇到一个组件内嵌两个组件,而这两个子组件所使用的数据来自于同一个API,如下图所示. 如果这时候两个子组件各自导入同一个接口就会显得多余.另外的办法是由父组件导入接口数据,再从父组件将接 ...
- vue 子组件传递数据跟父组件
子组件 <body> <div v-on:click="test"></div> <script> export default { ...
- vue $emit $on 从子组件传递数据给父组件
原理是: 子组件使用$emit发送数据,父组件使用$on,或者v-on绑定, 来监听子组件发送的数据. 子组件: <button @click="sendChildData" ...
- Vue子组件传递数据给父组件
子组件通过this.$emit(event,data)传递数据到父组件 以下是例子: father.vue 父组件 <template> <div> <child @ne ...
- Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法
一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...
- vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)
看了很多文章,官网文档也有看,对父子组件通信说的不是很明白:决定自己总结一下: vue一般都使用构建工具构建项目:这样每个组件都是单文件组件:而网上很多文章都是script标签方式映入vue,组件通信 ...
- vue子组件数据跟着父组件改变
父组件的代码 <template> <div class="home"> <img alt="Vue logo" src=&quo ...
- vuejs利用props,子组件修改父组件的数据,父组件修改子组件的的数据,数据类型为数组
博文参考 传送们点一点 父组件: <template> <div> <aa class="abc" v-model="test" ...
- vue之provide和inject跨组件传递属性值失败(父组件向子组件传值的两种方式)
简单介绍:当一个子组件需要用到父组件的父组件的某些参数.那么这个时候为了避免组件重复传参,使用vue的依赖注入是个不错的方法,直接在最外层组件设置一个provide,内部不管多少嵌套都可以直接取到最外 ...
随机推荐
- Docker + node(koa) + nginx + mysql 线上环境部署
在上一篇 Docker + node(koa) + nginx + mysql 开发环境搭建,我们进行了本地开发环境搭建 现在我们就来开始线上环境部署 如果本地环境搭建没有什么问题,那么线上部署的配置 ...
- java中implements和extends的区别
1,extends是继承某个类的,可以使用某个类的方法,也可以重写父类的方法. 2,implements是用于实现类接口,可以实现一个或多个类的接口,接口的方法一般为空的,所以必须重写这一个或多个的方 ...
- 【JDK1.8】 Java小白的源码学习系列:HashMap
目录 Java小白的源码学习系列:HashMap 官方文档解读 基本数据结构 基本源码解读 基本成员变量 构造器 巧妙的tableSizeFor put方法 巧妙的hash方法 JDK1.8的putV ...
- Unity3d组件实现令人惊叹的像素粒子特效!
我们先看看两个特效,感受一下,有没有学习的动力? =========================================================================== ...
- 6、python基本数据类型之序列类型
前言:python的基本数据类型可以分为三类:数值类型.序列类型.散列类型,本文主要介绍序列类型及其通用操作. 一.序列类型 1)字符串(str):用单引号('),双引号("),三引号(三单 ...
- 51Nod 1238 最小公倍数之和V3
题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...
- qt中的拖拽及其使用技巧
关于qt中的拖放操作,首先可以看这篇官方文档:http://doc.qt.io/qt-5.5/dnd.html 一.QDrag 首先是创建QDrag,可以在mousePressEvent或者mouse ...
- 聊一聊 MySQL 数据库中的那些锁
在软件开发中,程序在高并发的情况下,为了保证一致性或者说安全性,我们通常都会通过加锁的方式来解决,在 MySQL 数据库中同样有这样的问题,一方面为了最大程度的利用数据库的并发访问,另一方面又需要保证 ...
- Intent传递实现Parcelable接口的对象
Intent可以传递基本数据类型,在对象实现了Parcelable接口后,Intent也可以传递对象. 1. 使类ListVideo实现了Parcelable接口. package com.examp ...
- 脚本实现统计osd内的pg数量
脚本代码如下: ceph pg dump | awk ' /pg_stat/ { col=; while($col!=“up”) {col++}; col++ } /[-9a-f]+.[-9a-f]+ ...