vue中父子组件的相互传递

子组件从父组件那获取数据
在子组件中, 把他想要从父组件那得到的数据写在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>
运行结构:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue父子组件之间传递信息可以通过props和$emit事件来实现。 1. 父组件向子组件传递信息: 在父组件通过props将数据传递给子组件,子组件通过props接收数据即可。 父组件: ``` <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { message: 'Hello, child component!' } } } </script> ``` 子组件: ``` <template> <div> {{ message }} </div> </template> <script> export default { props: { message: String } } </script> ``` 2. 子组件向父组件传递信息: 在子组件通过$emit事件触发父组件的方法,将需要传递的数据作为参数传递即可。 子组件: ``` <template> <div> <button @click="sendMessage">Send message to parent</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('send-message', 'Hello, parent component!') } } } </script> ``` 父组件: ``` <template> <div> <child-component @send-message="receiveMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, methods: { receiveMessage(message) { console.log(message) } } } </script> ``` 在子组件通过$emit触发了send-message事件,父组件通过@send-message监听到事件,并且将传递的数据作为参数传递给了receiveMessage方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值