光之使者 发表于 2024-6-11 09:02:00

vue3 uniapp h5 安卓和iOS开发适配踩坑记录

https://img-blog.csdnimg.cn/093f837bc6fe413bb9dc9189354620ef.png
font-size适配屏幕巨细及iOS和安卓状态栏及安全隔断的处置惩罚

https://img-blog.csdnimg.cn/cb379194db8944f4981e34ef472ff2e2.png
https://img-blog.csdnimg.cn/db41d3be6f2a4772aeb5e435890b76e1.png
App.vue
<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import './main.scss'
onLaunch(() => {
console.log("App Launch");
varwidth = document.documentElement.clientWidth;
const fontSize = width / 750 + 'px';
// 这样写是不生效的h5编译运行的文件是有默认的style样式的
// 一个标签上不能同时出现两个style,不符合编译原则
// document.documentElement.style.fontSize = fontSize;

// 解决方法
// 1. 利用html的默认style来实现
// 2. 在html上添加--gobal-font-size(注意-gobal-font-size变量仅适用于css文件),设置文字的大小
      // 1. 在static 文件夹下创建 css/public.css 文件
      // 2. 在 main.ts 文件中引入 public.css (import './static/css/public.css';)
    document.documentElement.className = 'gobal-font-size';
// 3. 然后将设置文字大小的类添加到html标签上
    document.documentElement.style.setProperty("--gobal-font-size", fontSize);

// 处理系统不同兼容性问题
uni.getSystemInfo({
      success: function (res) {
          const statusBarHeight = res.statusBarHeight;
          document.documentElement.style.setProperty("--statusBarHeight", statusBarHeight + 'px');
          console.log('状态栏的高度', statusBarHeight)

          if (res.platform === "android") {
            document.body.classList.add('android');
          } else if (res.platform === "ios") {
            document.body.classList.add('ios');

            // 处理安全区域的距离
            const safeArea = res.safeArea;
            const bottomDistance = res.screenHeight - safeArea.bottom;
            document.documentElement.style.setProperty("--safe-area", bottomDistance + 'px');
            console.log('底部安全区域距离:', bottomDistance);

          } else {
            document.body.classList.add('other');
          }
      }
})
});
onShow(() => {
console.log("App Show");
});
onHide(() => {
console.log("App Hide");
});
</script>
<style>
@import "@/static/fonts/iconfont.css";
</style>
public.css
html {
    --gobal-font-size: 0.45px;
    /*状态栏的高度*/
    --statusBarHeight: 44px;
    /*在竖屏正方向下的安全区域*/
    --safe-area: 34px;
}
.gobal-font-size {
    font-size: var(--gobal-font-size) !important;
}

#app {
    padding-bottom: var(--safe-area);
}

/* 安卓兼容性单独处理*/
.android {
    /* 处理状态栏的间距 */
    padding-top: var(--statusBarHeight);
}
/* ios兼容性单独处理*/
.ios {
    /* 处理状态栏的间距 */
    padding-top: var(--statusBarHeight);
}
/* 其它兼容性单独处理*/
.other {

}
记得在main.ts中引入public.css哟~
https://img-blog.csdnimg.cn/8ed2529762dd4267a051e0ebb0f185fa.png
提醒:
能做全局的就万万不要单独写,后续维护是个麻烦。
iOS position:fixed失效

iOS的position: fixed是根据隔断最近的scroll来定位的, 假如自定义弹窗是通过position: fixed来实现的,就不要在scroll-view里面调用,可以通过自定义样式(overflow: auto)来实现滚动。
https://img-blog.csdnimg.cn/1ed18d60daf24612aea65034a3bf74a8.png
calc 100vh 计算高度

最好不要使用height: calc(100vh - 44px)这种方式来计算高度,系统不一样,高度的计算方法也不一样,iOS偶然候没标题,安卓会莫名的出现滚动条。
body 上使用 padding-top: env(safe-area-inset-top)

只针对主页有用,对tabbar页面无效,tabbar页面要单独处置惩罚。
页面设置100vh高度后Android会出现滚动条(body上添加padding处置惩罚状态栏的隔断后出现的标题)

解决方法:
body {
        box-sizing: border-box;
}
在安卓上浏览器会将body元素的宽高设置为视口的宽高,并将任何添加到body元素上的padding会增长在总宽高上面,就会出现滚动条。
pinyin包实现省市区的选择

使用pinyin-pro包比pinyin包小
https://img-blog.csdnimg.cn/24a1016409534772878e17fa2a4a80d2.png
https://img-blog.csdnimg.cn/4cd5ccf33ce44506bbd4cb1e638b33d4.png
实现方法:

[*]调用接口查察省市区的数据
[*]处置惩罚省市区的数据
import {pinyin} from 'pinyin-pro';
// 声明变量存储处理的数据
let list = [];
// 数据处理
if (res.data?.length > 0) {
        res.data?.map(item => {
                // 将中文字符串转换成拼音数组
                const pinyinArray = pinyin(item?.name, {
                        // 设置拼音首字母的风格
                        style: pinyin.STYLE_FIRST_LETTER
                })
                // 获取拼音数组的第一个字母(这里有些多音字翻译的不准确要单独处理)
                const firstLetter = pinyinArray;
                // 将获取的第一个字母转换成大写字母
                const upFont = firstLetter.toUpperCase();
                // 判断拼音数组的第一个字母是否存在在list中
                const listIndex = list.findIndex(i => i?.letter === upFont);
                if (listIndex > 0) {
                        // 相同首字母的数据合并在一起
                        list.data = .data];
                } else {
                        list.push({
                                letter: upFont,
                                data:
                        })
                }
        })
}
// 将处理过的数据从小到大排列
const sortList = list.sort((a, b) => {
        return a.letter.localeCompare(b.letter);
})
手机号实现344格式

https://img-blog.csdnimg.cn/8f10db075edc414bba243072932e0717.png
注意:必须用input才气实现时候号码格式化。
<input v-model="mobileText" @input="mobileChange"/>
<div class="clear" v-if="mobileText.length > 0"></div>

mobileChange() {
        // 手机号码
        this.mobile = e.detail.value.replace(/[^0-9_]/g, '');
        // 页面显示的数据
        this.mobileText = this.mobile;
        // 实现344格式
        if (this.mobileText.length > 3 && this.mobileText.length < 8) {
                this.mobileText = this.mobileText.replace(/^(\d{3})/g, '$1 ');
        } else if this.mobileClear.length > 7) {
                this.mobileText = this.mobileText.replace(/^(\d{3})(\d{4})/g, '$1 $2 ')
        }
}
uniapp 微信小步伐和h5的小区别之编译内容

最近碰到个微信小步伐要改造成h5的项目,微信小步伐的文件编译成h5文件之后样式丢失,就nth-child的样式,我原来还以为是配置错了,效果后面发现是编译的标签不一样,下面是一些编译的差别:
微信小步伐编译后的文件:
https://img-blog.csdnimg.cn/0b80631264334807bda3804f2dff687d.png
h5 编译后的文件(直接将所有的uniapp组件都编译成原生的view形式):
https://img-blog.csdnimg.cn/85206303f7c741828178eb298cdc4510.png
按钮微信小步伐编译之后是button,h5编译之后照旧uni-button。
表单 Uncaught (in promise) TypeError: Converting circular structure to JSON

这个标题我找了很久,要命~
https://img-blog.csdnimg.cn/direct/8e925f813c674f7a96a03448735ad9ec.png
透明度只能用小数不能用%

uniapp的opacity不正知识别%,只能用小数,而且安卓真机调试都正常,打包安装后透明度就会极低,70%险些看不到。
uni-calendar 的使用

https://img-blog.csdnimg.cn/direct/24a7c62fdd36458b84ca23fc0d12c5a9.png
<uni-calendar v-if="showCalendar" class="mt-4" :selected="selectedInfo" :showMonth="false" @change="change"
                  @monthSwitch="monthSwitch"/>
selectedInfo = [];
tasks = null;
showCalendar = false;

created() {
    this.selectedInfo = [];
    onLoad(option => {
      // 查询数据(掉接口查询数据)
      this.getData();
      // 等数据加载完了再显示日历 不然 日历打点加载不出来(要切换月份或者点今日按钮之后才会显示)
      setTimeout(() => {
            this.showCalendar = true;
      }, 1000)
    })
}

getData() {
        this.tasks = [...];
        this.selectedInfo = [
                {
                        date: '2024-03-15',
            info: '2条消息',
                }
        ];
        console.log('数据查询','日历打点','日历上的数据添加')
}

change(e) {
   console.log('点击的是哪一天', e.fulldate)
}

monthSwitch(e) {
   // 将查询的数据切换成切换的月份和年份
   // 切换月份改变数据
}
页面头部添加操纵按钮

https://img-blog.csdnimg.cn/direct/e74402d06a6146bea97d04dfab5f192b.png
在pages.json中配置:https://img-blog.csdnimg.cn/direct/36cd10a02b2f4d11bf6551eb4b6f81c4.png
https://img-blog.csdnimg.cn/direct/4c9b297e581748c9ac2622a663c98ada.png
https://img-blog.csdnimg.cn/direct/de67b655945d4cb885940b57d4eb0956.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: vue3 uniapp h5 安卓和iOS开发适配踩坑记录