(一) 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的更多相关文章

  1. 微信小程序将外部数据从父组件中传入到子组件

    小程序组件开发遇到一个组件内嵌两个组件,而这两个子组件所使用的数据来自于同一个API,如下图所示. 如果这时候两个子组件各自导入同一个接口就会显得多余.另外的办法是由父组件导入接口数据,再从父组件将接 ...

  2. vue 子组件传递数据跟父组件

    子组件 <body> <div v-on:click="test"></div> <script> export default { ...

  3. vue $emit $on 从子组件传递数据给父组件

    原理是: 子组件使用$emit发送数据,父组件使用$on,或者v-on绑定, 来监听子组件发送的数据. 子组件: <button @click="sendChildData" ...

  4. Vue子组件传递数据给父组件

    子组件通过this.$emit(event,data)传递数据到父组件 以下是例子: father.vue 父组件 <template> <div> <child @ne ...

  5. Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法

    一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...

  6. vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)

    看了很多文章,官网文档也有看,对父子组件通信说的不是很明白:决定自己总结一下: vue一般都使用构建工具构建项目:这样每个组件都是单文件组件:而网上很多文章都是script标签方式映入vue,组件通信 ...

  7. vue子组件数据跟着父组件改变

    父组件的代码 <template> <div class="home"> <img alt="Vue logo" src=&quo ...

  8. vuejs利用props,子组件修改父组件的数据,父组件修改子组件的的数据,数据类型为数组

    博文参考 传送们点一点 父组件: <template> <div> <aa class="abc" v-model="test" ...

  9. vue之provide和inject跨组件传递属性值失败(父组件向子组件传值的两种方式)

    简单介绍:当一个子组件需要用到父组件的父组件的某些参数.那么这个时候为了避免组件重复传参,使用vue的依赖注入是个不错的方法,直接在最外层组件设置一个provide,内部不管多少嵌套都可以直接取到最外 ...

随机推荐

  1. Flask蓝图(Blueprint)

    一.作用 1.目录结构划分 2.url添加前缀 url_prefix 3.应用特殊装饰器,在该蓝图定义的特殊装饰器,只在改蓝图的起效 二.简单示例 1.创建一个项目文件 2.创建一个同名的python ...

  2. 【python系统学习07】一张图看懂字典并学会操作

    点击跳转 - 原文地址 数据类型 - 字典(dict) 目录: 一张图get字典 字典是什么 js的对象 字典长啥样 语法伪代码 示例demo 语法成像 字典怎么用 字典长度获取--len函数 提取字 ...

  3. C入门题目

    37. 反转一个3位整数 反转一个只有3位数的整数. 样例 样例 1: 输入: number = 123 输出: 321 样例 2: 输入: number = 900 输出: 9 注意事项 你可以假设 ...

  4. lind 语 api 数据的安全性  第四弹

    web api的安全性怎么保证呢. 一般公司会自己封装一套请求的规范. 下面来看看lind语里的webapi安全规范 step one 先看下 diagram: 学而思: 从上面的图分析一下: 如果我 ...

  5. c#数字图像处理(七)直方图匹配

    直方图匹配,又称直方图规定化,即变换原图的直方图为规定的某种形式的直方图,从而使两幅图像具有类似的色调和反差.直方图匹配属于非线性点运算. 直方图规定化的原理:对两个直方图都做均衡化,变成相同的归一化 ...

  6. mac安装了anaconda但是在终端不能使用conda命令

    只需在终端输入如下命令即可 export PATH=~/anaconda3/bin:$PATH

  7. nmap详解之基础示例

    扫描主机target.example.com的所有TCP端口 nmap -v target.example.com 发起对target.example.com所在网络上的所有255个IP地址的秘密SY ...

  8. Linux下启动/关闭Oracle

    一.Linux下启动Oracle Linux下启动Oracle分为两步: 1)启动监听: 2)启动数据库实例: 1.登录服务器,切换到oracle用户,或者以oracle用户登录 [admin@dat ...

  9. IDEA | 更改idea打开新项目默认配置

    背景 使用过idea的童鞋应该都发现,用idea打开一个新项目,总是需要重新配置一遍,它会使用系统默认配置,例如maven.JDK等设置. 解决方案 IDEA其实有个设置是可以更改新项目的默认配置,大 ...

  10. 深入并发锁,解析Synchronized锁升级

    这篇文章分为六个部分,不同特性的锁分类,并发锁的不同设计,Synchronized中的锁升级,ReentrantLock和ReadWriteLock的应用,帮助你梳理 Java 并发锁及相关的操作. ...