具体参考 动态插槽名问题讨论和 HACK 方案

效果

注意微信小程序 动态插槽不支持默认内容

子组件 创建插槽

关键处代码
    <template v-for="(item, index) in tabItem"><view class="flex-grow h-0 overflow-auto" v-if="computedValue === item.name" :key="index"><!-- #ifdef MP --><!-- <slot name="{{item.name}}"> </slot> --><slot name="tab:{{index}}"> </slot><!-- #endif --><!-- #ifdef H5 || APP-PLUS  --><!-- <slot :name="item.name"> --><slot :name="`tab:${index}`"><view class="w-full h-80 flex-center"><text class="text-c_gray-2">暂无数据{{ item.name }}</text></view></slot><!-- #endif --></view></template>
完整代码 (样式缺失 使用的是tailwindcss)
<template><view class="w-full h-full flex flex-col"><view class="w-full bg_gradient_blue_4 flex flex-row items-center justify-start" style="height: 83rpx"><viewv-for="(item, index) in tabItem":key="index"style="height: 83rpx"class="flex-center flex-row rounded-t-20":style="computedWidthStyle":class="computedValue === item.name ? 'bg_withe_blue text-c_blue-2' : 'text-c_blue-3'"@click="changeTab(item)"><text>{{ item.label }}</text></view></view><template v-for="(item, index) in tabItem"><view class="flex-grow h-0 overflow-auto" v-if="computedValue === item.name" :key="index"><!-- #ifdef MP --><!-- <slot name="{{item.name}}"> </slot> --><slot name="tab:{{index}}"> </slot><!-- #endif --><!-- #ifdef H5 || APP-PLUS  --><!-- <slot :name="item.name"> --><slot :name="`tab:${index}`"><view class="w-full h-80 flex-center"><text class="text-c_gray-2">暂无数据{{ item.name }}</text></view></slot><!-- #endif --></view></template></view>
</template>
<script>
export default {props: {value: {// 必须要使用valuedefault: ''},tabItem: {type: Array,default: () => [// {//   label: '作业中',//   name: 'working',//   query: {//     name: 'tabItemName',//     value: 'working'//   }// },// {//   label: '待拣',//   name: 'waiting',//   query: {//     name: 'tabItemName',//     value: 'waiting'//   }// },// {//   label: '已拣',//   name: 'already',//   query: {//     name: 'tabItemName',//     value: 'already'//   }// },// {//   label: '拣货详情',//   name: 'details',//   query: {//     name: 'tabItemName',//     value: 'details'//   }// }]}},data: () => ({}),computed: {//   双向绑定computedValue: {get() {return this.value},set(value) {this.$emit('input', value)}},computedWidthStyle() {let length = this.tabItem.lengthif (length <= 0) {return ''}let w = ''w = Math.floor(100 / length).toFixed(5)return `width:${w}%;`}},methods: {changeTab(item) {this.$emit('input', item.name)}},watch: {},// 组件周期函数--监听组件挂载完毕mounted() {},// 组件周期函数--监听组件数据更新之前beforeUpdate() {},// 组件周期函数--监听组件数据更新之后updated() {},// 组件周期函数--监听组件激活(显示)activated() {},// 组件周期函数--监听组件停用(隐藏)deactivated() {},// 组件周期函数--监听组件销毁之前beforeDestroy() {}
}
</script>
<style></style>

父组件 使用插槽

使用关键代码
   <!-- #ifdef MP --><template v-for="(item, index) in tabItem" slot="tab:{{index}}"><!-- #endif  --><!-- #ifdef H5  || APP-PLUS   --><template v-for="(item, index) in tabItem" :slot="`tab:${index}`"><!-- #endif --><!-- 作业中 --><view :key="index"  v-if="getIndexToName(tabItem,'working',index)">1少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所所所所所所所所2+{{ item.name }}</view></template>

如果 插槽时渲染出现时出现一个 view 属性为空的标签包裹 包裹 插槽组件 导致子组件无法与父组件样式继承错乱 可使用如下写法 不需要使用 template 包裹 判断是使用v-show

        <view v-for="(item, index) in tabItem" :slot="`tab:${index}`" :key="index"  v-show="getIndexToName(tabItem,'working',index)">1少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所所所所所所所所2+{{ item.name }}</view>
完整代码 (样式缺失 使用的是tailwindcss)
<template><view class="w-full flex flex-col" :style="getTabStyle"><wmsTabs class="w-full h-0 flex-grow " :tabItem="tabItem" v-model="tabValue"><!-- #ifdef MP --><template v-for="(item, index) in tabItem" slot="tab:{{index}}"><!-- #endif  --><!-- #ifdef H5  || APP-PLUS   --><template v-for="(item, index) in tabItem" :slot="`tab:${index}`"><!-- #endif --><!-- 作业中 --><view :key="index"  v-if="getIndexToName(tabItem,'working',index)">1少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所所所所所所所所2+{{ item.name }}</view></template></wmsTabs></view>
</template>
<script>
import wmsTabs from '@/components/base-ui/wms-tabs'
export default {components: {wmsTabs},props: {// 组件显示区域最大高度maxHeight: Number | String},data: () => ({tabValue: 'working',tabItem: [{label: '作业中',name: 'working',query: {name: 'tabItemName',value: 'working'}},{label: '待拣',name: 'waiting',query: {name: 'tabItemName',value: 'waiting'}},{label: '已拣',name: 'already',query: {name: 'tabItemName',value: 'already'}},{label: '拣货详情',name: 'details',query: {name: 'tabItemName',value: 'details'}}]}),computed: {getTabStyle() {if (this.maxHeight && this.maxHeight >= 0) {return `height:${this.maxHeight}px;`}}},methods: {//   通过name获取index 下标getIndexToName(list,name,currentIndex){let index_ = '';for (let index = 0; index < list.length; index++) {const item = list[index];if(item.name===name){index_ = index;break;}}return index_ === currentIndex;}},watch: {},// 组件周期函数--监听组件挂载完毕mounted() {},// 组件周期函数--监听组件数据更新之前beforeUpdate() {},// 组件周期函数--监听组件数据更新之后updated() {},// 组件周期函数--监听组件激活(显示)activated() {},// 组件周期函数--监听组件停用(隐藏)deactivated() {},// 组件周期函数--监听组件销毁之前beforeDestroy() {}
}
</script>
<style></style>

uniapp 动态插槽 slot 兼容微信小程序 h5 APP相关推荐

  1. uni-app 实现 fullpage 组件(适用于微信小程序,h5等)

    uni-app 实现 fullpage 组件(适用于微信小程序,h5等) 业务需求. 本文github 源码地址 1.组件 src/components/FullPage/index.vue < ...

  2. 计算机毕业设计Python+uniapp扫码点餐微信小程序(小程序+源码+LW)

    计算机毕业设计Python+uniapp扫码点餐微信小程序(小程序+源码+LW) 该项目含有源码.文档.程序.数据库.配套开发软件.软件安装教程 项目运行 环境配置: Pychram社区版+ pyth ...

  3. uni-app(Vue.js)创建运行微信小程序

    uni-app(Vue.js)创建运行微信小程序 1.全局安装 npm install -g @vue/cli 需要安装node,官方网站,否则提示npm不可用 2.创建uni-app 新建文件夹,选 ...

  4. uniapp的打包:h5、微信小程序以及APP方式

    uniapp的打包:h5.微信小程序以及APP方式 H5打包 微信小程序打包 App打包 本人用的是HBuilder编译器,学习uniapp时b站某位大大推荐的,我刚开始接触代码时候也用过,那时候并不 ...

  5. 基于uniapp+vue+微信小程序+安卓app电影院订票小程序H5网站设计

    开发技术:uniapp + vue + ElementUI + 微信小程序 + 安卓app + Springboot 开发工具环境:HBuilder + 微信开发者工具 + VsCode + Idea ...

  6. 微信小程序/H5(UniApp)导入/导出excel文件

    微信小程序/H5本地读写excel 准备工作 JS库 本地导入并读取Excel(*.xls, *.xlsx) Excel文件示例 选取文件并获得binary数据 Binary数据转json 本地构建并 ...

  7. uni-app 微信小程序 模仿 app二层楼功能

    uni-app 微信小程序 模仿 app二层楼功能 先占个坑,今天应该写不完, 后续在慢慢补全 更新 终于写完了 这个的计算量很大,我自己的安卓机测试的时候一卡一卡的,公司同事的iphone是没有问题 ...

  8. 计算机毕业设计Python+uniapp在线小说阅读平台微信小程序(小程序+源码+LW)

    计算机毕业设计Python+uniapp在线小说阅读平台微信小程序(小程序+源码+LW) 该项目含有源码.文档.程序.数据库.配套开发软件.软件安装教程 项目运行 环境配置: Pychram社区版+ ...

  9. 二手市场回收基于微信小程序和app两种应用开发uniapp

    回收纸皮书籍.服装饰品.箱包鞋帽.床上用品.塑料制品.其他 ,微信小程序.app开发 源码下载地址:请点击下载

  10. Java后端对接微信支付(微信小程序、APP、PC端扫码)非常全,包含查单、退款

    首先我们要明确目标,我们点击 微信支付官网 ,我们主要聚焦于这三种支付方式,其中JSPAI与APP主要与uniapp开发微信小程序与APP对接,而NATIVE主要与网页端扫码支付对接 1.三种支付统一 ...

最新文章

  1. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
  2. ubuntu spyder 不能输入中文
  3. iOS Sharing #02 | 2019-03-30
  4. ConcurrentHashMap 原理解析
  5. 强势解析 eBay BASE 模式、去哪儿及蘑菇街分布式架构
  6. 麦肯锡方法中的经验(读书摘要)
  7. mhl数据线_mhl接口是什么?mhl接口有哪些作用
  8. 打印机多少钱一台?购买打印机打印速度快吗
  9. LiveData更新数据报错java.lang.IllegalStateException: Cannot invoke setValue on a background thread
  10. 安卓无线打印服务器,安卓 打印服务器
  11. 【VREP】四舵轮(or n舵轮)自旋与平移融合运动解算
  12. vue实现任务周期cron表达式选择组件
  13. Redis 官方可视化工具,功能真的强大
  14. 硬盘修复工具软件重建MBR
  15. MIT6.005 Problem Set 1 Tweet Tweet
  16. Minecraft 1.12.2 彩色渐变字体 模组发布
  17. 搜索引擎优化SEO的基本技术
  18. JS数组中的剩余方法
  19. Android项目打包成aar文件并在其他项目引用,作为依赖包
  20. python 爬虫 爬取当当网图书信息

热门文章

  1. 包含源文件 —— 是奇技淫巧还是饮鸩止渴?
  2. 【弄nèng - SearchGuard】应用篇 —— Windows下elasticsearch5.2.2 安装SearchGuard5.2.2
  3. 老九课堂c语言百度云,老九学堂C语言
  4. java excel 数组公式_教你如何快速学习Excel数组公式及运用
  5. js实现oss批量下载文件_前端实现批量打包下载文件
  6. el-table使用span-method合并行
  7. 目标检测入门(一)两阶段目标检测的由来
  8. MFC自绘带背景颜色标题栏
  9. Web实时语音/视频聊天/文件传输
  10. java拼图_java实现拼图游戏