vue3 子父组件、组件传值、

目录

父组件给子组件传值

子组件给父组件传值


页面index.vue   子组件footer.vue

父组件给子组件传值

index.vue页面

<div class="box">
    <!-- 页面使用组件的地方-->
    <!-- 
        parentData 子组件通过 parentData 接收
        test 要传给子组件的数据
    -->
    <bottom :parentData = "test"/>
</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>

相关推荐

  1. vue3 组件组件

    2023-12-05 16:56:01       36 阅读
  2. vue3组件如何给组件

    2023-12-05 16:56:01       6 阅读
  3. vue3组件组件,并在组件接受

    2023-12-05 16:56:01       19 阅读
  4. Vue实现组件组件

    2023-12-05 16:56:01       18 阅读
  5. VUE组件组件进行

    2023-12-05 16:56:01       10 阅读
  6. 黑豹程序员-vue3 setup 组件组件

    2023-12-05 16:56:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 16:56:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 16:56:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 16:56:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 16:56:01       20 阅读

热门阅读

  1. Vue3-admin-template的表格合计计算

    2023-12-05 16:56:01       39 阅读
  2. 针对Arrays.asList的坑,可以有哪些处理措施

    2023-12-05 16:56:01       45 阅读
  3. mac如何永久设置环境变量

    2023-12-05 16:56:01       43 阅读
  4. React - 表单组件实现

    2023-12-05 16:56:01       39 阅读
  5. Diary18-Word文本部件

    2023-12-05 16:56:01       36 阅读
  6. GnuCash macos 设置中文的方法

    2023-12-05 16:56:01       38 阅读
  7. 第四十篇v-if vs v-show

    2023-12-05 16:56:01       42 阅读
  8. SiR-NHS酯偶联物:用于细胞过程定量分析 星戈瑞

    2023-12-05 16:56:01       32 阅读
  9. 什么是数据分析

    2023-12-05 16:56:01       44 阅读