vue如何通过插槽组件之间数据传递

 更新时间:2024年06月07日 11:41:28   作者:阿伍.  
这篇文章主要介绍了vue如何通过插槽组件之间数据传递问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue通过插槽组件之间数据传递

1.父向子传值

//--->Father.vue
<template>
  <div id="father">
    <p>黄色为父组件---data的值为:{{ data }}</p>
    <Son>
        <template #usname>
            <p>data的值为:{{data}}</p>//将父组件的data数据传入子组件的<slot>插槽
        </template>
    </Son>
  </div>
</template>
 
<script>
import Son from "./Son.vue";
export default {
  data() {
    return {
      data: "Father",
    };
  },
  components: {
    Son,
  },
};
</script>
 
<style scoped>
#father {
  width: 400px;
  height: 400px;
  background-color: yellow;
}
</style>
 
//--->son.vue
<template>
  <div id="son">
    <p>蓝色为子组件</p>
    <slot name="usname"></slot>
  </div>
</template>
 
<script>
export default {};
</script>
 
<style scoped>
#son {
  width: 200px;
  height: 200px;
  background-color: skyblue;
}
</style>

2.子向父传值

//--->Father.vue
<template>
  <div id="father">
    <p>黄色为父组件---data的值为:{{ data }}</p>
    <button @click="upp">1</button>//点击查看传过来的值
    <Son>
      <template #usname="obj">//通过v-slot指令接收son.vue传过来的值
        <p>data的值为:{{ data }}</p>
        <p>son_data的值为{{ obj.son }}</p>
        <button @click="son_data = obj.son">1</button
        >//通过点击按钮将传过来的值保存在Father组件的son_data数据中
      </template>
    </Son>
  </div>
</template>
 
<script>
import Son from "./Son.vue";
export default {
  data() {
    return {
      data: "Father",
      son_data: "",
    };
  },
  components: {
    Son,
  },
  methods: {
    upp() {
      console.log(this.son_data);
    },
  },
};
</script>
 
<style scoped>
#father {
  width: 400px;
  height: 400px;
  background-color: yellow;
}
</style>
//--->Son.vue
<template>
  <div id="son">
    <p>蓝色为子组件</p>
    <slot name="usname" :son='son_data'></slot>//添加自定义属性son将son_data数据传入父组件
  </div>
</template>
 
<script>
export default {
    data(){
        return{
            son_data:'Son'
        }
    }
};
</script>
 
<style scoped>
#son {
  width: 200px;
  height: 200px;
  background-color: skyblue;
}
</style>

vue跨组件动态插槽传递

在看coderwhy老师后台管理系统项目实战视频的时候发现组件封装的思路与动态插槽传递非常有意思,记录一下!

需求及基本信息说明

组件Goods调用组件page-content,组件page-content调用组件hy-table。

为了方便使用,组件page-content封装公共插槽(如果将所有页面的私有的插槽都一股脑放到组件page-content中封装性会变差),需要在Goods中传递私有插槽内容在hy-table中显示。

这时候就产生了跨组件插槽传递的需求,而由于编译作用域限制的原因,Goods组件中的#image不能直接被hy-table接收,而需要先由page-content接收goods中的数据,再由page-content将数据传递到hy-table中。

而实际开发中不可能在page-content中将插槽名称写死,上面图例page-content中只是简单拿#image进行举例,实际开发中可以在组件page-content中动态插入私有插槽,实现如下。

代码

  • Goods.vue

Goods中使用组件page-content并传入配置文件contentTableConfig,并向page-content中名字为image的插槽提供内容<el-image>

<template>
  <div class="goods">
    <page-content :contentTableConfig="contentTableConfig" pageName="goods">
      <template #image="scope">
        <el-image
          style="width: 60px; height: 60px"
          :src="scope.row.imgUrl"
          :preview-src-list="[scope.row.imgUrl]"
        >
        </el-image>
      </template>
      <template #oldPrice="scope">{{ '¥' + scope.row.oldPrice }}</template>
    </page-content>
  </div>
</template>
  • contentTableConfig配置文件数据

其中slotName为status、createAt、updateAt、handler为公共插槽配置

export const contentTableConfig = {
  title: '商品列表',
  propList: [
    { prop: 'name', label: '商品名称', minWidth: '80' },
    { prop: 'oldPrice', label: '原价格', minWidth: '80', slotName: 'oldPrice' },
    { prop: 'newPrice', label: '现价格', minWidth: '80' },
    { prop: 'imgUrl', label: '商品图片', minWidth: '100', slotName: 'image' },
    { prop: 'status', label: '状态', minWidth: '100', slotName: 'status' },
    {
      prop: 'createAt',
      label: '创建时间',
      minWidth: '250',
      slotName: 'createAt'
    },
    {
      prop: 'updateAt',
      label: '更新时间',
      minWidth: '250',
      slotName: 'updateAt'
    },
    { label: '操作', minWidth: '120', slotName: 'handler' }
  ],
  showIndexColumn: true,
  showSelectColumn: true
}
  • page-content.vue

定义一个函数otherPropSlots对配置信息进行过滤,去掉公共的插槽名称,剩下就是私有的插槽名称了,返回一个包含私有插槽的数组,在template中对这个数组进行遍历,slot :name="私有插槽名"接收Goods中的内容,“#私有插槽名”的template向子组件hy-table传递内容

<template>
  <div class="page-content">
    <hy-table
      v-bind="contentTableConfig"
    >
 
      <template #status="scope">
        <el-button
          plain
          size="mini"
          :type="scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler>
        <div class="handle-btns">
          <el-button v-if="isUpdate" icon="el-icon-edit" size="mini" type="text"
            >编辑</el-button
          >
          <el-button
            v-if="isDelete"
            icon="el-icon-delete"
            size="mini"
            type="text"
            >删除</el-button
          >
        </div>
      </template>
 
      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
 
    </hy-table>
  </div>
</template>
 
<script lang="ts">
import { defineComponent } from 'vue'
import HyTable from '@/base-ui/table'
 
export default defineComponent({
  components: {
    HyTable
  },
  props: {
    contentTableConfig: {
      type: Object,
      require: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  setup(props) {
    // 获取其他的动态插槽名称
    const otherPropSlots = props.contentTableConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )
    return {
      otherPropSlots
    }
  }
})
</script>

想说的话:

动态传递插槽和封装的思路绝了,需要花好多功夫和时间去消化~

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue3中正确使用ElementPlus的示例代码

    Vue3中正确使用ElementPlus的示例代码

    这篇文章主要介绍了Vue3中正确使用ElementPlus的示例代码,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • vue系列之动态路由详解【原创】

    vue系列之动态路由详解【原创】

    下面小编就为大家带来一篇vue系列之动态路由详解【原创】。小编觉得挺不错的,现在就想给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • vue通过路由实现页面刷新的方法

    vue通过路由实现页面刷新的方法

    本篇文章主要介绍了vue通过路由实现页面刷新的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • vue项目首屏打开速度慢的解决方法

    vue项目首屏打开速度慢的解决方法

    这篇文章主要介绍了vue项目首屏打开速度慢的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • vue的webcamjs集成方式

    vue的webcamjs集成方式

    这篇文章主要介绍了vue的webcamjs集成方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • VUE3 data()函数详解

    VUE3 data()函数详解

    Vue 3的data()函数是选项式 API 中声明响应式数据的入口,核心是通过返回对象配合 Proxy 实现响应式,接下来通过本文介绍VUE3 data()函数的相关知识,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • 详解如何制作并发布一个vue的组件的npm包

    详解如何制作并发布一个vue的组件的npm包

    这篇文章主要介绍了详解如何制作并发布一个vue的组件的npm包,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 详解Vue3-pinia状态管理

    详解Vue3-pinia状态管理

    这篇文章主要介绍了Vue3-pinia状态管理,pinia是 vue3 新的状态管理工具,简单来说相当于之前 vuex,它去掉了 Mutations 但是也是支持 vue2 的,需要的朋友可以参考下
    2022-08-08
  • Vue突然报错doesn‘t work properly without JavaScript enabled的解决方法

    Vue突然报错doesn‘t work properly without JavaScript enabled

    最近在做项目的时候遇到了些问题,所以这篇文章主要给大家介绍了关于Vue突然报错doesn‘t work properly without JavaScript enabled的解决方法,需要的朋友可以参考下
    2023-01-01
  • Vue的弹出框实现图片预览和视频预览功能

    Vue的弹出框实现图片预览和视频预览功能

    本文介绍基于Vue3的媒体预览组件,支持图片与视频预览,具备缩放、旋转、拖拽功能,采用ElementUI Dialog容器,通过计算属性实现动态样式,优化拖拽性能,自动调整方向,便于集成使用
    2025-08-08

最新评论