uni-app开发路上的坑

前情提要

记录一下使用uni-app + Vue3开发项目过程中遇到问题、以及解决方法。

map组件使用问题

skew属性

配置skew属性后个别手机(vivo iqoo neo11)中心点或者缩放异常(设置了坐标在广东,打开地图却定位在北京,或者定位对了但是缩放错误),未找到确切原因,为保证应用正常使用,删除了skew配置。

触摸事件穿透

设置了元素定位在地图上方时,滑动元素,底部地图会跟随移动,常见于苹果手机,通过设置以下代码即可:

html 复制代码
<view @touchmove.stop.prevent="stopTouchMove"></view>
js 复制代码
const stopTouchMove = () => {}

Wot UI FloatingPanel浮动面板交互优化

当FloatingPanel包含滚动列表时,在内容区向上滑动,不会触发面板展开,只会滚动列表,不符合使用的习惯,应该改为:内容区向上滑动,判断面板是否完全展开,是则列表滚动,否则展开面板

html 复制代码
<!-- 父组件 传入子组件浮动面板的高度、展开高度 -->
<wd-floating-panel v-model:height="height" :anchors="anchors" :safeAreaInsetBottom="true" :showScrollbar="false">
    <PromotionList :panelHeight="height" :panelMaxHeight="panelMaxHeight" />
</wd-floating-panel>

<!-- 子组件 -->
<scroll-view style="height: 100%;" :scroll-y="scrollY"></scroll-view>
js 复制代码
// 子组件,设置计算属性scrollY,判断浮动面板高度达到最大高度则允许垂直方向的滚动,否则禁用
const props = defineProps({
    // 浮动面板高度
    panelHeight: {
        type: Number,
        default: 0
    },
    // 浮动面板最大高度
    panelMaxHeight: {
        type: Number,
        default: 0
    }
})

const scrollY = computed(() => {
    return props.panelHeight === props.panelMaxHeight
})

启用分享功能

通过混入(mixin)方式设置指定页面启用分享功能

js 复制代码
// shareMixin.js
export default {
  data() {
    return {
      share: {
        title: "标题",
        path: "页面路由",
      },
    };
  },
  // 分享到微信好友功能
  onShareAppMessage(res) {
    // 获取当前页面标题
    const pages = getCurrentPages();
    const data = pages[pages.length - 1]?.$page;
    const { title, path } = this.share;
    return {
      title,
      path: data?.fullPath || path,
      success(res) {
        uni.showToast({
          title: "分享成功",
        });
      },
      fail(res) {
        uni.showToast({
          title: "分享失败",
          icon: "none",
        });
      },
    };
  },
  // 分享到朋友圈功能
  onShareTimeline(res) {
    const pages = getCurrentPages();
    const data = pages[pages.length - 1]?.$page;
    const { title, path } = this.share;
    return {
      title,
      path: data?.fullPath || path,
      success(res) {
        uni.showToast({
          title: "分享成功",
        });
      },
      fail(res) {
        uni.showToast({
          title: "分享失败",
          icon: "none",
        });
      },
    };
  },
};

富文本内容格式化

富文本字符串包含的图片可能设置了固定宽度,影响预览效果,通过正则进行处理

js 复制代码
export function formatRichImg(content) {
  if (!content) return "";
  return content
    .replace(/(<img\b[^>]*?)\s+style=(["'])[\s\S]*?\2([^>]*>)/gi, "$1$3")
    .replace(/<img/gi, '<img style="max-width:100%; height:auto;"');
}
相关推荐
崔庆才丨静觅10 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606110 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了11 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅11 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅11 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅11 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment12 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅12 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊12 小时前
jwt介绍
前端
爱敲代码的小鱼12 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax