特別注意:
※:在components下的組件,圖片路徑用 /static/img/back.png 這樣的根路徑形式,不要用../static 或者 ../../static 的形式,不然很坑,有些平台不報錯也不顯示,有些找不到路徑。
tips:防止彈窗遮罩時頁面可滾動,在彈窗的外層view標簽加上 @touchmove.stop.prevent=""
1、關於自定義導航欄中的劉海屏適配問題:
官方提供了一個CSS變量可以直接引用:
var(--status-bar-height)
該變量自動匹配設備平台狀態欄高度
此變量可以用calc() 加上其他單位數值來使用
具體參數和說明:官方使用自定義導航欄注意事項
2、swiper中高度無法自適應時,采用動態獲取節點賦值給外層swiper組件
uni.createSelectorQuery()后加.in(this)可以防止app端出錯
<swiper :indicator-dots="true" :style="{height:listHeight+'px'}" :autoplay="false" :interval="3000" :duration="1000"></swiper>
var _self; export default { data() { return { listHeight:215 } }, onLoad() { _self=this; _self.getEleHeight('.swiper-item'); }, onShow() { }, methods: { getEleHeight(list){ let info = uni.createSelectorQuery().in(_self).select(list); info.boundingClientRect(function(data) { //data - 各種參數 if(data != null){ _self.listHeight = data.height; }else{ setTimeout(function(){ _self.getEleHeight('.swiper-item'); },300) } }).exec() } } }
3、橫向scroll-view隨子元素寬度自適應
關鍵在於給scroll-view的直接下一層view設置如下css:
width:auto;
display: inline-block;
white-space: nowrap;
<scroll-view scroll-x="true" class="scroll_box"> <view class="list"> <view class="item" v-for="(item,index) of 4" :key="index"> </view> </view> </scroll-view>
.scroll_box{ width: 100%; height: auto; } .list{ width: auto; height: 100upx; display: inline-block; white-space: nowrap; } .item{ width:320upx; display: inline-block; height: 100%; }
4、部分組件向上滑動超出屏幕時固定在頂部,仿餓了么吸頂
給該組件設置css定位元素position的值為sticky,可以結合top和left值來調節位置。
5、關於tabbar的一些情況
建議使用配置的tabbar,自定義的view沒有緩存機制。
原生tabbar其實很多功能,參考讀完以下知識點能實現大部分需求:
tabbar文檔API方法:https://uniapp.dcloud.io/api/ui/tabbar
tabbar官網詳解:https://uniapp.dcloud.io/collocation/pages?id=tabbar
6、保存圖片到本地
真機親測至少安卓有用,更多請查看:uni圖片保存本地(app和微信小程序端)
uni.showModal({ title: '提示', content: '確定保存到相冊嗎', success: function (res) { if (res.confirm) { uni.downloadFile({ url: _self.ewmImg,//圖片地址 success: (res) =>{ if (res.statusCode === 200){ uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function() { uni.showToast({ title: "保存成功", icon: "none" }); }, fail: function() { uni.showToast({ title: "保存失敗", icon: "none" }); } }); } } }) } else if (res.cancel) { } } });
7、app端動態修改原生導航欄信息
// #ifdef APP-PLUS var pages = getCurrentPages(); var page = pages[pages.length - 1]; var currentWebview = page.$getAppWebview(); var tn = currentWebview.getStyle().titleNView; tn.buttons[0].text = "自定義 "; tn.buttons[0].color ="#333333"; currentWebview.setStyle({ titleNView: tn }); // #endif
8、部分常用功能參考地址如下
登錄和圖片批量上傳可借鑒下方鏈接
uni-app 前后端實戰課 - 《悅讀》:http://www.hcoder.net/tutorials/info_1371.html