uniapp动态插槽打包成小程序不生效?

最近写uniapp打包成h5和微信小程序端,在使用插槽时遇到了各种问题,记录一下

  1. 动态插槽在小程序不生效,
  2. 使用插槽,小程序端样式错乱,
  3. 使用v-if语法,插槽默认值不生效

1. 动态插槽在小程序不生效

定义插槽

js 复制代码
// #ifdef H5
<slot :name="`content-${index}`"></slot>
// #endif 
// #ifdef MP-WEIXIN
<slot name="content-{{index}}"></slot>
// #endif

使用插槽

js 复制代码
// #ifdef H5
<template
   v-for="(item, index) in formData"
   v-slot:[`content-${index}`]
   :key="item.id"
>
// #endif 
// #ifdef MP-WEIXIN
<view
   v-for="(item, index) in formData"
   slot="content-{{index}}"
   :key="item.id"
>
// #endif

按网上的方法,还是不显示,我把template改成view就显示可以了。

2. 使用插槽,小程序端样式错乱

小程序在使用插槽时会默认在最外层加view,在使用时发现默认插槽加上#default也会,可能就会导致样式错乱。

默认插槽通过以下写法可以避免包裹一层view,不过最外层还是会有一层组件名路径的标签,如果还是错乱,还是要通过另外写样式来兼容

如果是具名插槽,也只能另外写样式处理,目前没找到好的解决方案

js 复制代码
// #ifdef H5
<template #default>
// #endif
// #ifdef MP-WEIXIN
<template>
// #endif 

3.使用v-if语法,插槽默认值不生效

遍历展示时,有的数据需要使用插槽,有的不需要

一开始我是这样写的,结果所有的数据都有slot标签,只是有的是空的,默认值就展示不出来

js 复制代码
<Cell v-for="item in formData">
  <template v-if="item.type"></template>
</Cell>

只能通过以下方式写,但增加了代码量,如果遇上更复杂的插槽会更麻烦,也是没找到好的解决方案

js 复制代码
<view v-for="item in formData">
  <Cell v-if="item.type">
    <template></template>
  </Cell>
  <Cell v-else>
    <template ></template>
  </Cell>
</view>

纯做记录,如果jy们有好的方案,欢迎评论

相关推荐
kq-blue2 小时前
uniapp开发小程序,导出文件打开并保存,实现过程downloadFile下载,openDocument打开
小程序·uni-app·notepad++
走,带你去玩3 小时前
uniapp 微信小程序在线引入字体图标
微信小程序·小程序·uni-app
Q_Q19632884756 小时前
python+uniapp基于微信小程序的高校二手商品交易系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
Heyuan_Xie7 小时前
uni-app-配合iOS App项目开发apple watch app
uni-app·apple watch app·uni-app系统教程
莫空00008 小时前
uView Plus TabBar完美适配iOS安全区:告别底部小横条遮挡烦恼!
微信小程序·uni-app
万岳软件开发小城10 小时前
基于Uniapp+PHP的教育培训系统开发指南:网校源码实战剖析
uni-app·php·软件开发·在线教育系统源码·教育平台搭建·教育app开发·网校小程序
從南走到北1 天前
设备巡检系统小程序ThinkPHP+UniApp
微信小程序·小程序·uni-app·微信公众平台
刘大浪1 天前
uniapp 实战新闻页面(一)
android·uni-app
我是shenzhongchaoii1 天前
写100个前端效率工具(1):uni-app海报生成 uni-wxml2canvas
uni-app