vue中父子组件的相互传递
2018-01-18 16:13
465 查看
子组件从父组件那获取数据
在子组件中, 把他想要从父组件那得到的数据写在props中
在父组件中, 调用子组件像这样 <xxx :m='message'></xxx>
父组件从子组件那获取数据
在子组件中,触发函数调用this.$emit('事件名', 传递的数据)
在父组件中, 在调用子组件时 <xxx @事件名='getVal'></xxx> 注:getVal(val){this.val=val} 为method内一函数
实例如下:
实现功能介绍:点击父组件按钮设置isShow的值,传给子组件,通过判断isShow的值控制子组件是否显示,如果子组件显示则显示父组件传过来的noticeConten的值,子组件提交输入框中输入的数据给父组件显示
父组件:
<template> <div> <input class="notice_input" type="text" v-model="noticeContent" placeholder="父 输入框"> <button class="input_btn" @click="refWay">点击显示</button> <notice-plugin :is-show='isShow' :notice-content='noticeContent' @is-show-p='getIsShow' @son-input-m='getSonM'></notice-plugin> <div class="show_son" v-if="isShowMe"> 子给父的数据: {{sonMessage}} </div> </div></template>
<script> export default{ data () { return { noticeContent: '', isShow: false, isShowMe: false, sonMessage: '' } }, methods: { refWay () { this.isShow = true }, getIsShow (val) { this.isShowMe = val // val就是从子组件获取的数据 }, getSonM (val) { this.sonMessage = val // val就是从子组件获取的数据 } } }</script>子组件:<template> <div class="notice_box" v-if="isShow"> <div>父给子的数据:{{noticeContent}}</div> <div> <input class="notice_input" type="text" v-model="sonInputM" placeholder="子 输入框"> <button class="input_btn" @click="sendToParent">点击显示</button> </div> </div></template><script> export default { name: 'notice-plugin', props: { //存放子组件从父组件获取的数据 isShow: { type: Boolean, default: false }, noticeContent: { type: String, default: '' } }, data () { return { isShowParent: false, sonInputM: '' } }, methods: { sendToParent () { this.isShowParent = true this.$emit('is-show-p', this.isShowParent) // 传递数据给父组件 this.$emit('son-input-m', this.sonInputM) // 传递数据给父组件 } } }</script>运行结构:
在子组件中, 把他想要从父组件那得到的数据写在props中
在父组件中, 调用子组件像这样 <xxx :m='message'></xxx>
父组件从子组件那获取数据
在子组件中,触发函数调用this.$emit('事件名', 传递的数据)
在父组件中, 在调用子组件时 <xxx @事件名='getVal'></xxx> 注:getVal(val){this.val=val} 为method内一函数
实例如下:
实现功能介绍:点击父组件按钮设置isShow的值,传给子组件,通过判断isShow的值控制子组件是否显示,如果子组件显示则显示父组件传过来的noticeConten的值,子组件提交输入框中输入的数据给父组件显示
父组件:
<template> <div> <input class="notice_input" type="text" v-model="noticeContent" placeholder="父 输入框"> <button class="input_btn" @click="refWay">点击显示</button> <notice-plugin :is-show='isShow' :notice-content='noticeContent' @is-show-p='getIsShow' @son-input-m='getSonM'></notice-plugin> <div class="show_son" v-if="isShowMe"> 子给父的数据: {{sonMessage}} </div> </div></template>
<script> export default{ data () { return { noticeContent: '', isShow: false, isShowMe: false, sonMessage: '' } }, methods: { refWay () { this.isShow = true }, getIsShow (val) { this.isShowMe = val // val就是从子组件获取的数据 }, getSonM (val) { this.sonMessage = val // val就是从子组件获取的数据 } } }</script>子组件:<template> <div class="notice_box" v-if="isShow"> <div>父给子的数据:{{noticeContent}}</div> <div> <input class="notice_input" type="text" v-model="sonInputM" placeholder="子 输入框"> <button class="input_btn" @click="sendToParent">点击显示</button> </div> </div></template><script> export default { name: 'notice-plugin', props: { //存放子组件从父组件获取的数据 isShow: { type: Boolean, default: false }, noticeContent: { type: String, default: '' } }, data () { return { isShowParent: false, sonInputM: '' } }, methods: { sendToParent () { this.isShowParent = true this.$emit('is-show-p', this.isShowParent) // 传递数据给父组件 this.$emit('son-input-m', this.sonInputM) // 传递数据给父组件 } } }</script>运行结构:
相关文章推荐
- Vue.js 父子组件相互传递数据
- vue 父子组件属性传递
- Vue 父子组件的数据传递、修改和更新方法
- vue组件间通信、数据传递(父子组件,同级组件)
- vue-父子组件间的相互传值与传址
- 前端框架vue.js系列(7):父子组件参数变化传递规范
- vue中非父子组件之间传递值
- vue 父子组件之间的数据传递
- 关于vue.js父子组件数据传递
- vue.js---父子组件之间简单的数据传递
- vue学习记录——父子组件间传递数据
- vue2.0父子组件间传递数据的方法
- vue父子组件传递信息
- react都这么无情了,vue还是那么有义,4种父子组件数据双向传递大法
- VUE父子组件之间值传递
- Vue 父子组件传递方式
- vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由
- Vue 父子组件传递方式
- vue基础语法以及父子组件如何相互传值
- vue父子组件的数据传递示例