Vue 父组件主动获取子组件的数据和方法 子组件主动获取父组件的数据和方法

111111
https://blog.csdn.net/qq_39338967/article/details/130484676
在这里插入图片描述
父组件主动获取子组件的数据和方法=============
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
父组件主动获取子组件的数据和方法
在这里插入图片描述
父组件主动获取子组件的数据和方法==================
在这里插入图片描述
在这里插入图片描述
1、SubVue.vue子組件:

<template>
   <div class="subVue">
         {{msg}}
   </div>
</template>

<script>
export default {
    data(){
        return{
           msg: 'SubVue.vue',
           subTitle: '子組件中的变量--subTitle标题!'
        }
    },
    methods:{
       subMt(){
       
          console.log("==当前子页面==子組件中的无参方法--subVue--subMt()--不传参方法");
       },
       subParamMt(param){
        
          console.log("==当前子页面==子組件中的有参方法--subVue--subParamMt(param)
--传参["+param+"]方法");
       }
    }
}
</script>
<style>

 .subVue{
    
    background:rgb(83, 177, 83);
    width:800px;
    height:200px;
    font-size:40px;
    margin-top:200px;
 
 }
</style>

2、ParentVue.vue

<template>
  <div class="parentView">
      {{msg}}
      <br>
        // ref=”自定义组件名”
       <v-subvue ref="subvue"></v-subvue>
       <br>
       <button  @click="sx()">调用子组件的属性msg\subTitle</button>
       <button  @click="noparams()">调用子组件的无参方法</button>
       <button  @click="hasparams('123456')">调用子组件的有参方法</button>
  
  </div>
</template>

<script>

import SubVue  from  '@/views/SubVue'
export default {
data(){
    return{
     
     msg:'ParentView.vue'
    }
},
components:{
    'v-subvue':SubVue
},
methods:{
   sx(){
       //父组件调用子组件中的subtitle属性;
       //子組件中的msg參數
       this.$refs.subvue.msg;
       console.log("==当前子页面=====子組件中的msg參數==="+this.$refs.subvue.msg);
       this.$refs.subvue.subTitle;
       console.log("==当前子页面=====子組件中的subTitle參數= =="+this.$refs.subvue.subTitle);
   },
   noparams(){
       this.$refs.subvue.subMt();
      
   },
   hasparams(params){
       this.$refs.subvue.subParamMt(params);
   }
}
}
</script>
<style>
 .parentView{
    background:honeydew;
    width:800px;
    height:200px;
    font-size:40px;
 }
</style>

在这里插入图片描述
子组件主动获取父组件的数据和方法

=子组件主动获取父组件的属性和方法============
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue组件获取组件数据,你可以通过以下几种方式实现: 1. 使用 `ref` 引用组件并直接访问其数据:在组件中,使用 `ref` 属性给组件命名,然后通过该引用来访问组件数据。例如: ```html <template> <div> <child-component ref="child"></child-component> <button @click="getChildData">获取组件数据</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { getChildData() { const childData = this.$refs.child.childData; console.log(childData); } } } </script> ``` 在上面的示例中,我们通过 `ref` 属性将组件命名为 "child",然后在 `getChildData` 方法中通过 `this.$refs.child` 来访问组件实例,并获取其 `childData` 数据。 2. 使用自定义事件:在组件中,通过 `$emit` 方法触发一个自定义事件,并将数据作为参数传递给组件组件通过监听该自定义事件来获取组件数据。例如: ```html <!-- ChildComponent.vue --> <template> <div> <button @click="sendData">发送数据组件</button> </div> </template> <script> export default { data() { return { childData: '组件数据' } }, methods: { sendData() { this.$emit('childData', this.childData); } } } </script> ``` ```html <!-- ParentComponent.vue --> <template> <div> <child-component @childData="getChildData"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { getChildData(childData) { console.log(childData); } } } </script> ``` 在上面的示例中,组件通过 `$emit` 方法触发了一个名为 "childData" 的自定义事件,并将 `childData` 数据作为参数传递给组件组件通过 `@childData` 监听该自定义事件,并在 `getChildData` 方法获取组件传递过来的数据。 这些是获取组件数据的常用方法,你可以根据自己的需求选择合适的方式来实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值