本文介绍: 父组件给子组件传值页面index.vue 子组件footer.vue父组件给子组件传值index.vue页面子组件 footer.vue

目录

父组件给子组件传值

子组件给父组件传值


页面index.vue   子组件footer.vue

父组件给子组件传值

index.vue页面

<div class="box">
    <!-- 页面使用组件的地方--&gt;
    <!-- 
        parentData 子组件通过 parentData 接收
        test 要传给子组件的数据
    --&gt;
    <bottom :parentData = "test"/&gt;
</div>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import bottom from './footer.vue' //引入子组件footer
// test 数据自己随便定义
const test = ref({
  id:299709,
  img_url: "https://res.a.com/pc/common/images/list404.jpg",
  name: "test",
  mobile: "10086",
})
</script>

子组件 footer.vue

<div>如果数据需要处理直接用即可:{{parentData.name}}</div>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'

const props = defineProps<{
  parentData: Object
}>()

const newParentData = ref({})
onMounted(() => {
    //如果js需要处理父组件传过来的值 
    // 直接处理数据 props.parentData  重新赋给新newParentData 用即可
    newParentData.value = 处理后的数据 
})
</script>

子组件给父组件传值

子组件footer.vue

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
// const emit = defineEmits([父组件要接收key])
const emit = defineEmits(['childData'])
onMounted(() => {
  setTimeout(()=>{//模拟获取数据
      //在能获取到值可以传递的放进行传值 emit('childBrokerData', 要传递的值)
      let jsonData = {name:"test",id:1}  
      emit('childData', jsonData)
  },3000)  
})
</script>

父组件 index.vue

<bottom @childData="getChildData"/>


<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import bottom from './footer.vue' //引入子组件footer

const getChildData = (e) => {
  console.log(e,'======子组件传过来数据')
}
</script>

原文地址:https://blog.csdn.net/qq_39109182/article/details/134723760

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18507.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注