uni-app开发中的各种问题处理
<p><span style="font-size: 14pt">特别注意:</span></p><p><span style="font-size: 14pt"> ※:在components下的组件,图片路径用 <span style="color: rgba(255, 0, 0, 1)">/static/img/back.png</span> 这样的根路径形式,不要用../static 或者 ../../static 的形式,不然很坑,有些平台不报错也不显示,有些找不到路径。</span></p>
<p> </p>
<p><span style="font-size: 14pt">tips:防止弹窗遮罩时页面可滚动,在弹窗的外层view标签加上 <span style="color: rgba(255, 0, 0, 1)">@touchmove.stop.prevent=""</span></span></p>
<p><span style="font-size: 14pt"> </span></p>
<p><span style="font-size: 14pt">1、关于自定义导航栏中的刘海屏适配问题:</span></p>
<p>官方提供了一个CSS变量可以直接引用:</p>
<p>var(--status-bar-height)</p>
<p>该变量自动匹配设备平台状态栏高度</p>
<p>此变量可以用calc() 加上其他单位数值来使用</p>
<p>具体参数和说明:官方使用自定义导航栏注意事项</p>
<p> </p>
<p><span style="font-size: 14pt">2、swiper中高度无法自适应时,采用动态获取节点赋值给外层swiper组件</span></p>
<pre><span>uni.createSelectorQuery()后加.in(this)可以防止app端出错</span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">swiper </span><span style="color: rgba(255, 0, 0, 1)">:indicator-dots</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> :style</span><span style="color: rgba(0, 0, 255, 1)">="{height:listHeight+'px'}"</span><span style="color: rgba(255, 0, 0, 1)"> :autoplay</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> :interval</span><span style="color: rgba(0, 0, 255, 1)">="3000"</span><span style="color: rgba(255, 0, 0, 1)"> :duration</span><span style="color: rgba(0, 0, 255, 1)">="1000"</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">swiper</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"> var</span><span style="color: rgba(0, 0, 0, 1)"> _self;
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
listHeight:</span>215<span style="color: rgba(0, 0, 0, 1)">
}
},
onLoad() {
_self</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
_self.getEleHeight(</span>'.swiper-item'<span style="color: rgba(0, 0, 0, 1)">);
},
onShow() {
},
methods: {
getEleHeight(list){
let info </span>=<span style="color: rgba(0, 0, 0, 1)"> uni.createSelectorQuery().in(_self).select(list);
info.boundingClientRect(</span><span style="color: rgba(0, 0, 255, 1)">function</span>(data) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">data - 各种参数</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(data != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
_self.listHeight </span>=<span style="color: rgba(0, 0, 0, 1)"> data.height;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
setTimeout(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
_self.getEleHeight(</span>'.swiper-item'<span style="color: rgba(0, 0, 0, 1)">);
},</span>300<span style="color: rgba(0, 0, 0, 1)">)
}
}).exec()
}
}
}</span></pre>
</div>
<p><span style="font-size: 14pt"> 3、横向scroll-view随子元素宽度自适应</span></p>
<p> 关键在于给scroll-view的直接下一层view设置如下css:</p>
<p> width:auto;</p>
<p> display: inline-block;</p>
<p> <em id="__mceDel">white-space: nowrap;</em></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">scroll-view </span><span style="color: rgba(255, 0, 0, 1)">scroll-x</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="scroll_box"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="list"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="item"</span><span style="color: rgba(255, 0, 0, 1)"> v-for</span><span style="color: rgba(0, 0, 255, 1)">="(item,index) of 4"</span><span style="color: rgba(255, 0, 0, 1)"> :key</span><span style="color: rgba(0, 0, 255, 1)">="index"</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">scroll-view</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">.scroll_box</span>{<span style="color: rgba(255, 0, 0, 1)">
width</span>:<span style="color: rgba(0, 0, 255, 1)"> 100%</span>;<span style="color: rgba(255, 0, 0, 1)">
height</span>:<span style="color: rgba(0, 0, 255, 1)"> auto</span>;
}<span style="color: rgba(128, 0, 0, 1)">
.list</span>{<span style="color: rgba(255, 0, 0, 1)">
width</span>:<span style="color: rgba(0, 0, 255, 1)"> auto</span>;<span style="color: rgba(255, 0, 0, 1)">
height</span>:<span style="color: rgba(0, 0, 255, 1)"> 100upx</span>;<span style="color: rgba(255, 0, 0, 1)">
display</span>:<span style="color: rgba(0, 0, 255, 1)"> inline-block</span>;<span style="color: rgba(255, 0, 0, 1)">
white-space</span>:<span style="color: rgba(0, 0, 255, 1)"> nowrap</span>;
}<span style="color: rgba(128, 0, 0, 1)">
.item</span>{<span style="color: rgba(255, 0, 0, 1)">
width</span>:<span style="color: rgba(0, 0, 255, 1)">320upx</span>;<span style="color: rgba(255, 0, 0, 1)">
display</span>:<span style="color: rgba(0, 0, 255, 1)"> inline-block</span>;<span style="color: rgba(255, 0, 0, 1)">
height</span>:<span style="color: rgba(0, 0, 255, 1)"> 100%</span>;
}</pre>
</div>
<p><span style="font-size: 14pt"> </span></p>
<p><span style="font-size: 14pt">4、uniapp配置原生导航栏的字体图标</span></p>
<p>例如配置阿里巴巴字体图标库里的图标:选择要用的图标加入购物车,然后点购物车中的下载代码,得到其中的iconfont.css和iconfont.ttf,拷贝到项目使用即可。</p>
<p>代码如下:text属性的值就是iconfont.css里面对应图标的content值(<span style="color: rgba(255, 0, 0, 1)">需‘\u’开头</span>),对应官方文档:https://uniapp.dcloud.io/collocation/pages?id=app-titlenview-buttons</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> "app-plus":{
"titleNView":{
"buttons":[
{
"text": "\ue67e",
"fontSrc": "/static/iconfont/iconfont.ttf",
"fontSize": "22px",
"width":"auto"
}
]
}
}</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1157406/202009/1157406-20200911093516585-137630740.png"></p>
<p> </p>
<p><span style="font-size: 14pt">5、关于tabbar的一些情况</span></p>
<p>建议使用配置的tabbar,自定义的view没有缓存机制。</p>
<p>如果是子页面要做tabbar,可以用组件形式合并到一个页面去,分别控制显示隐藏。</p>
<p>原生tabbar其实很多功能,参考读完以下知识点能实现大部分需求:</p>
<p>tabbar文档API方法:https://uniapp.dcloud.io/api/ui/tabbar</p>
<p>tabbar官网详解:https://uniapp.dcloud.io/collocation/pages?id=tabbar</p>
<p> </p>
<p><span style="font-size: 14pt">6、保存图片到本地</span></p>
<p>真机亲测至少安卓有用,更多请查看:uni图片保存本地(app和微信小程序端)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> uni.showModal({
title: </span>'提示'<span style="color: rgba(0, 0, 0, 1)">,
content: </span>'确定保存到相册吗'<span style="color: rgba(0, 0, 0, 1)">,
success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (res) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.confirm) {
uni.downloadFile({
url: _self.ewmImg,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">图片地址</span>
success: (res) =><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (res.statusCode === 200<span style="color: rgba(0, 0, 0, 1)">){
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
uni.showToast({
title: </span>"保存成功"<span style="color: rgba(0, 0, 0, 1)">,
icon: </span>"none"<span style="color: rgba(0, 0, 0, 1)">
});
},
fail: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
uni.showToast({
title: </span>"保存失败"<span style="color: rgba(0, 0, 0, 1)">,
icon: </span>"none"<span style="color: rgba(0, 0, 0, 1)">
});
}
});
}
}
})
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.cancel) {
}
}
});</span></pre>
</div>
<p><span style="font-size: 14pt">7、app端动态修改原生导航栏信息</span></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef APP-PLUS</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> pages =<span style="color: rgba(0, 0, 0, 1)"> getCurrentPages();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> page = pages;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> currentWebview =<span style="color: rgba(0, 0, 0, 1)"> page.$getAppWebview();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> tn =<span style="color: rgba(0, 0, 0, 1)"> currentWebview.getStyle().titleNView;
tn.buttons[</span>0].text = "自定义"<span style="color: rgba(0, 0, 0, 1)">;
tn.buttons[</span>0].color ="#333333"<span style="color: rgba(0, 0, 0, 1)">;
currentWebview.setStyle({
titleNView: tn
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span></pre>
</div>
<p> </p>
<p><span style="font-size: 14pt">8、官方富文本编辑器editor(目前除了视频音频等未实现大部分常用功能都得到了支持)</span></p>
<p><span style="font-size: 14pt">组件链接:https://uniapp.dcloud.io/component/editor</span></p>
<p><span style="font-size: 18.6667px">API链接:https://uniapp.dcloud.io/api/media/editor-context</span></p>
<p>注意事项:</p>
<p>保存富文本的html内容时,如果图片进行了手动伸缩大小,建议对标签内的图片width属性处理成<span style="color: rgba(255, 0, 0, 1)">百分比</span>,默认是具体像素值,无法兼容不同屏幕宽度。</p>
<p>解析富文本内容可以用v-html 或者 rich-text 属性,个人推荐v-html,之前测试时rich-text不能解析带下划线的文字,也可以去插件市场找好的插件解析。</p>
<p> </p>
<p><span style="font-size: 14pt">9、app资源升级更新方案地址:https://ask.dcloud.net.cn/article/34972</span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
有人住高楼,有人处深沟。
有人光万丈,有人一生绣。
时光是匆匆,回首无旧梦。
人生若几何,凡尘事非多。
深情总遗却,妄自也洒脱。<br><br>
来源:https://www.cnblogs.com/nanyang520/p/11758187.html
頁:
[1]