子组件获取父组件数据 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,内部不管多少嵌套都可以直接取到最外 ...
随机推荐
- mong 按 geometry 搜索 地理位置信息
看 地理位置索引的使用 $near $geometry
- OpenCV2.4.13+Qt5.6.2配置方法
[1.环境变量] D:\Soft\OpenCV2\MinGW_build\bin; C:\Qt\Qt5.6.2\Tools\mingw492_32\bin; D:\Soft\Programming\C ...
- Shiro身份验证及授权(二)
一.Shiro 身份验证 身份验证的步骤: 收集用户身份 / 凭证,即如用户名 / 密码: 调用 Subject.login 进行登录,如果失败将得到相应的 AuthenticationExcepti ...
- chrome浏览器无法开启同步功能 request cancel
解决办法 添加代理规则*.googleapis.com
- springboot中使用Caffeine本地缓存
Caffeine是使用Java8对Guava缓存的重写版本性能有很大提升 一 依赖 <dependency> <groupId>org.springframework.boot ...
- 解决git报错:error: RPC failed; curl 18 transfer closed with outstanding read data remaining 的方法
报错信息: error: RPC failed; curl 18 transfer closed with outstanding read data remainingfatal: the remo ...
- webpack入门系列2
前面介绍了使用webpack做最基础的打包,接下来讲讲webpack的进阶. 1.使用 webpack 构建本地服务器: 想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webp ...
- sougn开源,自己写的一个项目
源代码:http://yun.baidu.com/share/link?shareid=1504480803&uk=2739888323 联系方式:1034465036@qq.com
- VC运行时库/MD、/MDd、/MT、/MTd说明
http://blog.csdn.net/holybin/article/details/26134153 VC运行时库设置:VC项目属性->配置属性->C/C++->代码生成-&g ...
- ipwry源码
qqwry.ipwry都是cnss(http://blog.csdn.net/cnss/article/details/136069)出品,终于找到了源码,下载地址:http://download.c ...