文章目錄
- 前言
- 一、設備相關
- 1.系統資訊
- 2.網路狀態
- 3.加速度計
- 4.撥打電話
- 5.掃碼
- 6.剪貼板
- 7.螢屏
- 7.振動
- 8.手機聯系人
- 二、導航設定
- 三、下拉和上拉
- 1.下拉重繪
- 2.案例--上拉加載更多
- 四、跨端兼容
- 五、互動反饋
- 1.uni.showToast(OBJECT)和uni.hideToast()
- 2.uni.showLoading(OBJECT)和uni.hideLoading()
- 3.uni.showModal(OBJECT)
- 4.uni.showActionSheet(OBJECT)
- 總結
2021年的第一篇博客,愿自己2020年重在參與、2021年重在脫單,祝各位讀者如牛般舞出自己的錦繡前程、如牛般游出自己的甜美愛情、如牛般點燃自己的美麗心情,
前言
本文主要介紹了介面的擴展應用:設備相關的介面包括獲取系統資訊、網路狀態、撥打電話、掃碼等;導航欄的動態設定;下拉重繪和上拉加載更多的實作;用條件編譯實作小程式、APP等多端兼容;提示框、Loading、模態彈窗等的互動反饋,
一、設備相關
1.系統資訊
uni.getSystemInfo(OBJECT)介面用來異步獲取系統資訊,
OBJECT常見引數和含義如下:
| 引數名 | 型別 | 是否必填 | 說明 |
|---|---|---|---|
| success | Function | 是 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
success回傳的常見引數和含義如下:
| 引數 | 說明 |
|---|---|
| brand | 手機品牌 |
| model | 手機型號 |
| pixelRatio | 設備像素比 |
| screenWidth | 螢屏寬度 |
| screenHeight | 螢屏高度 |
| windowWidth | 可使用視窗寬度 |
| windowHeight | 可使用視窗高度 |
| windowTop | 可使用視窗的頂部位置 |
| windowBottom | 可使用視窗的底部位置 |
| version | 引擎版本號 |
hello uniapp專案中,index.vue如下:
<template>
<view>
<button type="primary" @click="getinfo">獲取系統資訊</button>
</view>
</template>
<script>
export default {
data() {
return {
}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
getinfo: function(){
uni.getSystemInfo({
success:function(res){
console.log(res)
}
})
}
}
}
</script>
<style>
</style>
顯示:

可以獲取到當前設備比較全面的資訊,
除了使用uni.getSystemInfo(OBJECT)異步獲取設備資訊,還可以使用uni.getSystemInfoSync()同步獲取系統資訊;
uni.canIUse(String)可用于判斷應用的 API、回呼、引數、組件等是否在當前版本可用,
2.網路狀態
uni.getNetworkType(OBJECT)用來獲取網路型別,
OBJECT常見引數如下:
| 引數名 | 型別 | 必填 | 說明 |
|---|---|---|---|
| success | Function | 是 | 介面呼叫成功,回傳網路型別 networkType |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.onNetworkStatusChange(CALLBACK)用于監聽網路狀態變化,
CALLBACK回傳引數及含義如下:
| 引數 | 型別 | 說明 |
|---|---|---|
| isConnected | Boolean | 當前是否有網路連接 |
| networkType | String | 網路型別 |
如下:
<template>
<view>
<button type="primary" @click="getNetworkType">獲取網路型別</button>
</view>
</template>
<script>
export default {
data() {
return {}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
getNetworkType: function(){
uni.getNetworkType({
success:function(res){
console.log(res.networkType);
}
});
uni.onNetworkStatusChange(function(res){
console.log(res)
})
},
}
}
</script>
<style>
</style>
顯示:

可以看到,獲取到了當前的網路型別,
3.加速度計
uni.onAccelerometerChange(CALLBACK)用于監聽加速度資料,頻率為5次/秒,介面呼叫后會自動開始監聽,可使用uni.offAccelerometer取消監聽,
CALLBACK 回傳引數和含義如下:
| 引數 | 型別 | 說明 |
|---|---|---|
| x | Number | X 軸 |
| y | Number | Y 軸 |
| z | Number | Z 軸 |
uni.startAccelerometer(OBJECT)用于開始監聽加速度資料,
OBJECT引數和含義如下:
| 引數名 | 型別 | 默認 | 必填 | 說明 |
|---|---|---|---|---|
| interval | String | normal | 否 | 介面呼叫成功的回呼 |
| success | Function | 無 | 否 | 介面呼叫成功的回呼 |
| fail | Function | 無 | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 無 | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.stopAccelerometer(OBJECT)用于停止監聽加速度資料,
OBJECT 引數和含義如下:
| 引數名 | 型別 | 必填 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
4.撥打電話
uni.makePhoneCall(OBJECT)用于撥打電話,
OBJECT 引數如下:
| 引數名 | 型別 | 必填 | 說明 |
|---|---|---|---|
| phoneNumber | String | 是 | 需要撥打的電話號碼 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
如下:
<template>
<view>
<button type="primary" @click="tel">撥打電話</button>
</view>
</template>
<script>
export default {
data() {
return {}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
tel: function(){
uni.makePhoneCall({
phoneNumber: '10086'
})
},
}
}
</script>
<style>
</style>
顯示:

可以看到,模擬了撥打電話,
除了撥打電話,還可以實作發送短信等,
5.掃碼
uni.scanCode(OBJECT)用于調起客戶端掃碼界面,并在掃碼成功后回傳對應的結果,
OBJECT 引數及其含義如下:
| 引數名 | 型別 | 必填 | 說明 |
|---|---|---|---|
| onlyFromCamera | Boolean | 否 | 是否只能從相機掃碼,不允許從相冊選擇圖片 |
| scanType | Array | 否 | 掃碼型別,引數型別是陣列,二維碼是’qrCode’,一維碼是’barCode’,DataMatrix是‘datamatrix’,pdf417是‘pdf417’ |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式(識別失敗、用戶取消等情況下觸發) |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
其中,success 回傳引數如下:
| 引數 | 說明 |
|---|---|
| result | 所掃碼的內容 |
| scanType | 所掃碼的型別 |
| charSet | 所掃碼的字符集 |
| path | 當所掃的碼為當前應用的合法二維碼時,會回傳此欄位,內容為二維碼攜帶的 path |
簡單使用如下:
<template>
<view>
<button type="primary" @click="sca">掃描二維碼</button>
</view>
</template>
<script>
export default {
data() {
return {}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
sca: function(){
uni.scanCode({
success:function(res){
console.log(res)
}
})
},
}
}
</script>
<style>
</style>
6.剪貼板
uni.setClipboardData(OBJECT)用于設定系統剪貼板的內容,
OBJECT引數和含義如下:
| 引數名 | 型別 | 必填 | 說明 |
|---|---|---|---|
| data | String | 是 | 需要設定的內容 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.getClipboardData(OBJECT)用于獲取系統剪貼板內容,
OBJECT 引數和含義如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
如下:
<template>
<view>
<button type="primary" @click="sca">復制文字</button>
<text>{{txt}}</text>
</view>
</template>
<script>
var _self;
export default {
data() {
return {
txt: "hello"
}
},
onLoad() {
_self = this
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
sca: function(){
uni.setClipboardData({
data: 'https://blog.csdn.net/CUFEECR',
success:function(res){
console.log(res);
uni.getClipboardData({
success:function(gres){
console.log(gres.data)
_self.txt = gres.data
}
})
}
})
},
}
}
</script>
<style>
</style>
顯示:

7.螢屏
uni.setScreenBrightness(OBJECT)用于設定螢屏亮度,
OBJECT 引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| value | Number | 是 | 螢屏亮度值,范圍 0~1,0 最暗,1 最亮 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.getScreenBrightness(OBJECT)用于獲取螢屏亮度,
OBJECT 引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.setKeepScreenOn(OBJECT)用于設定是否保持常亮狀態,僅在當前應用生效,離開應用后設定失效,
OBJECT 引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| keepScreenOn | Boolean | 是 | 是否保持螢屏常亮 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
例如:
<template>
<view>
<button type="primary" @click="srn">設定螢屏亮度</button>
</view>
</template>
<script>
var _self;
export default {
data() {
return {
txt: "hello"
}
},
onLoad() {
_self = this
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
srn: function(){
uni.setScreenBrightness({
value: 0.1,
success:function(){
console.log('set success')
}
})
},
}
}
</script>
<style>
</style>
7.振動
uni.vibrate(OBJECT)用于使手機發生振動,
OBJECT 引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.vibrateLong(OBJECT)用于使手機發生較長時間的振動(400ms),uni.vibrateShort(OBJECT)用于使手機發生較短時間的振動(15ms),OBJECT引數與uni.vibrate(OBJECT)相同,
8.手機聯系人
uni.addPhoneContact(OBJECT)呼叫后,用戶可以選擇將該表單以“新增聯系人”或“添加到已有聯系人”的方式,寫入手機系統通訊錄,完成手機通訊錄聯系人和聯系方式的增加,
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| photoFilePath | String | 否 | 頭像本地檔案路徑 |
| lastName | String | 否 | 姓氏 |
| firstName | String | 是 | 名字 |
| mobilePhoneNumber | String | 否 | 手機號 |
| workPhoneNumber | String | 否 | 作業電話 |
| String | 否 | 電子郵件 | |
| url | String | 否 | 網站 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
二、導航設定
之前導航欄是通過配置實作的,但是不夠靈活,這時可以使用介面實作導航欄,
uni.setNavigationBarTitle(OBJECT)用于動態設定當前頁面的標題,
OBJECT引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| title | String | 是 | 頁面標題 |
| success | Function | 否 | 介面呼叫成功的回呼 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.setNavigationBarColor(OBJECT)用于設定頁面導航條顏色,如果需要進入頁面就設定顏色,請延遲執行,防止被框架內設定顏色邏輯覆寫,
OBJECT引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| frontColor | String | 是 | 前景顏色值,包括按鈕、標題、狀態欄的顏色,僅支持 #ffffff 和 #000000 |
| backgroundColor | String | 是 | 背景顏色值,有效值為十六進制顏色 |
| animation | Object | 否 | 影片效果,{duration,timingFunc} |
| success | Function | 否 | 介面呼叫成功的回呼函式 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.showNavigationBarLoading(OBJECT)用于在當前頁面顯示導航條加載影片,uni.hideNavigationBarLoading(OBJECT)在當前頁面隱藏導航條加載影片,
它們的OBJECT引數如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼函式 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
示例如下:
<template>
<view>
<button type="primary" @click="setTitle">設定標題</button>
</view>
</template>
<script>
var _self;
export default {
data() {
return {
txt: "hello"
}
},
onLoad() {
uni.showNavigationBarLoading();
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
setTitle: function(){
uni.setNavigationBarTitle({
title: 'hello...'
});
uni.hideNavigationBarLoading();
},
}
}
</script>
<style>
</style>
顯示:

可以看到,實作了設定標題和控制加載,
三、下拉和上拉
1.下拉重繪
onPullDownRefresh是一個處理函式,和onLoad等生命周期函式同級,用于監聽該頁面用戶下拉重繪事件,
使用前,需要在 pages.json 里,找到的當前頁面的pages節點,并在 style 選項中開啟 enablePullDownRefresh;
當處理完資料重繪后,uni.stopPullDownRefresh 可以停止當前頁面的下拉重繪,
uni.startPullDownRefresh(OBJECT)用于開始下拉重繪,呼叫后觸發下拉重繪影片,效果與用戶手動下拉重繪一致,
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| success | Function | 否 | 介面呼叫成功的回呼函式 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
uni.stopPullDownRefresh()用于停止當前頁面下拉重繪,
如下:
<template>
<view>
<view v-for="(item, index) in newslist" class="newslist">{{item}}</view>
</view>
</template>
<script>
var _self;
export default {
data() {
return {
newslist: []
}
},
onLoad() {
_self = this
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
onPullDownRefresh() {
this.getNews()
},
methods: {
getNews: function() {
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page=1',
success:function(res){
console.log(res);
var newslist = res.data.split('--hcSplitor--');
_self.newslist = newslist;
uni.stopPullDownRefresh();
uni.hideNavigationBarLoading();
}
})
}
}
}
</script>
<style>
.newslist {
line-height: 2em;
padding: 20px;
}
</style>
顯示:

可以看到,實作了下拉重繪加載資料,
2.案例–上拉加載更多
上拉加載更多有兩種實作方式:
- 通過scroll-view組件,識別滾動區域,滾動到底部出發加載事件;
- 識別頁面滾動到底部來觸發加載事件,
這里使用第二種方式,即生命周期函式onReachBottom來實作,即滾動條滾動到底部時觸發事件,
初步實作如下:
<template>
<view>
<view v-for="(item, index) in newslist" class="newslist">{{item}}</view>
</view>
</template>
<script>
// 添加page全域變數
var _self, page;
export default {
data() {
return {
newslist: []
}
},
onLoad() {
_self = this
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
onPullDownRefresh() {
this.getNews()
},
onReachBottom() {
this.getMoreNews()
},
methods: {
getNews: function() {
page = 1;
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page='+page,
success:function(res){
console.log(res);
var newslist = res.data.split('--hcSplitor--');
_self.newslist = _self.newslist.concat(newslist);
uni.stopPullDownRefresh();
uni.hideNavigationBarLoading();
page++;
}
})
},
getMoreNews: function() {
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page='+page,
success:function(res){
console.log(res);
uni.hideNavigationBarLoading();
if(res.data == null){
return false
};
var newslist = res.data.split('--hcSplitor--');
_self.newslist = newslist;
uni.stopPullDownRefresh();
page++;
}
})
}
}
}
</script>
<style>
.newslist {
line-height: 2em;
padding: 20px;
}
</style>
其中,添加全域變數page用于指定需要請求的資料的頁數;
定義函式分別實作第一次獲取資料和加載更多資料,
顯示:

可以看到,加載了2頁資料后,就不能再加載資料了,
此時還可以進行完善,如添加“加載更多”文本提示,
如下:
<template>
<view>
<view v-for="(item, index) in newslist" class="newslist">{{item}}</view>
<view class="loading">{{loadingText}}</view>
</view>
</template>
<script>
// 添加page、timer全域變數
var _self, page, timer = null;
export default {
data() {
return {
newslist: [],
loadingText: "下拉加載"
}
},
onLoad() {
_self = this
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
onPullDownRefresh() {
this.getNews()
},
onReachBottom() {
if(timer != null){
clearTimeout(timer)
};
timer = setTimeout(function(){
_self.getMoreNews()
}, 500);
},
methods: {
getNews: function() {
page = 1;
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page='+page,
success:function(res){
console.log(res);
var newslist = res.data.split('--hcSplitor--');
_self.newslist = _self.newslist.concat(newslist);
uni.stopPullDownRefresh();
uni.hideNavigationBarLoading();
page++;
}
})
},
getMoreNews: function() {
if(_self.loadingText == "已加載完畢"){
return false
};
_self.loadingText = "加載中";
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page='+page,
success:function(res){
console.log(res);
uni.hideNavigationBarLoading();
if(res.data == null){
_self.loadingText = "已加載完畢";
return false
};
var newslist = res.data.split('--hcSplitor--');
_self.newslist = newslist;
uni.stopPullDownRefresh();
_self.loadingText = "加載更多";
page++;
}
})
}
}
}
</script>
<style>
.newslist {
line-height: 2em;
padding: 20px;
}
.loading {
line-height: 2em;
text-align: center;
color: #DD524D;
margin-top: 30px;
}
</style>
使用延時器讓頁面先渲染完,再加載資料;
同時在getMoreNews函式中,先判斷是否加載完畢,如果已加載完畢則可以不再執行該函式,
顯示:

顯然,此時表現更好,
四、跨端兼容
很多時候,每個平臺有自己的一些特性,小程式和APP上實作是有一定區別的,可能不一定能兼容所有平臺,
此時需要使用條件編譯,即用特殊的注釋作為標記,在編譯時根據這些特殊的注釋,將注釋里面的代碼編譯到不同平臺,即使用#ifdef、#ifndef和#endif來判斷平臺型別,其中:
| 符號 | 含義 |
|---|---|
| #ifdef | if defined 僅在某平臺存在 |
| #ifndef | if not defined 除了某平臺均存在 |
| %PLATFORM% | 平臺名稱 |
對于API、組件、樣式等,有不同的注釋方式,具體如下:
| 方式 | 適用平臺 |
|---|---|
| API和pages.json | // #ifdef PLATFORM和// #endif |
| 組件 | <!-- #ifdef PLATFORM -->和<!-- #endif --> |
| 樣式 | /* #ifdef PLATFORM */和/* #endif */ |
測驗如下:
<template>
<view>
<!-- #ifdef MP-WEIXIN -->
<view class="wx">微信小程式</view>
<!-- #endif -->
<!-- #ifdef APP-PLUS -->
<view class="h5">H5+APP</view>
<!-- #endif -->
</view>
</template>
<script>
export default {
data() {
return {
}
},
onLoad() {
//#ifdef MP-WEIXIN
console.log('wx...')
//#endif
//#ifdef APP-PLUS
console.log('app...')
//#endif
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
}
}
</script>
<style>
</style>
顯示:

顯然,判斷出了當前為微信小程式平臺,
五、互動反饋
互動反饋包括提示框、加載等的設定,
1.uni.showToast(OBJECT)和uni.hideToast()
分別用于顯示和隱藏訊息提示框,
OBJECT引數和含義如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| title | String | 是 | 提示的內容,長度與 icon 取值有關 |
| icon | String | 否 | 圖示,有效值詳見下方說明, |
| image | String | 否 | 自定義圖示的本地路徑 |
| mask | Boolean | 否 | 是否顯示透明蒙層,防止觸摸穿透,默認:false |
| duration | Number | 否 | 提示的延遲時間,單位毫秒,默認:1500 |
| position | String | 否 | 純文本輕提示顯示位置,填寫有效值后只有 title 屬性生效, 有效值詳見下方說明, |
| success | Function | 否 | 介面呼叫成功的回呼函式 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
2.uni.showLoading(OBJECT)和uni.hideLoading()
前者用于顯示 loading 提示框,需主動呼叫后者才能關閉提示框,
OBJECT引數和含義如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| title | String | 是 | 提示的內容 |
| mask | Boolean | 否 | 是否顯示透明蒙層,防止觸摸穿透,默認:false |
| success | Function | 否 | 介面呼叫成功的回呼函式 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
測驗如下:
<template>
<view>
<button type="default" @click="showToast">顯示提示框</button>
<button type="default" @click="showLoading">顯示并關閉Loading提示框</button>
</view>
</template>
<script>
export default {
data() {
return {
}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
showToast: function(){
uni.showToast({
title: 'hello...',
icon: 'success'
})
},
showLoading: function(){
uni.showLoading({
title: 'loading...',
mask: true,
success:function(){
setTimeout(function(){
uni.hideLoading()
}, 3000)
}
})
}
}
}
</script>
<style>
</style>
顯示:

可以看到,可正常顯示、關閉提示框和loading,
3.uni.showModal(OBJECT)
用于顯示模態彈窗,類似于標準 html 的訊息框alert、confirm,
OBJECT引數和含義如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| title | String | 否 | 提示的標題 |
| content | String | 否 | 提示的內容 |
| showCancel | Boolean | 否 | 是否顯示取消按鈕,默認為 true |
| cancelText | String | 否 | 取消按鈕的文字,默認為"取消",最多 4 個字符 |
| cancelColor | HexColor | 否 | 取消按鈕的文字顏色,默認為"#000000" |
| confirmText | String | 否 | 確定按鈕的文字,默認為"確定",最多 4 個字符 |
| confirmColor | HexColor | 否 | 確定按鈕的文字顏色,H5平臺默認為"#007aff",微信小程式平臺默認為"#3CC51F",百度小程式平臺默認為"#3c76ff" |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
4.uni.showActionSheet(OBJECT)
?用于顯示操作選單,
OBJECT引數和含義如下:
| 引數名 | 型別 | 必填與否 | 說明 |
|---|---|---|---|
| itemList | Array | 是 | 按鈕的文字陣列 |
| itemColor | HexColor | 否 | 按鈕的文字顏色,字串格式,默認為"#000000" |
| success | Function | 否 | 介面呼叫成功的回呼函式,詳見回傳引數說明 |
| fail | Function | 否 | 介面呼叫失敗的回呼函式 |
| complete | Function | 否 | 介面呼叫結束的回呼函式(呼叫成功、失敗都會執行) |
測驗如下:
<template>
<view>
<button type="default" @click="showModal">顯示模態彈窗</button>
<button type="default" @click="showActionSheet">?顯示操作選單</button>
</view>
</template>
<script>
var actions = ['Music', 'Reading'];
export default {
data() {
return {
}
},
onLoad() {
},
onShow() {
console.log('index onshow')
},
onHide() {
console.log('index onhide')
},
methods: {
showModal: function(){
uni.showModal({
title: 'hello...',
content: 'Modal Window',
success:function(res){
if(res.confirm){
console.log('Confirm')
}else if(res.cancel){
console.log('Cancel')
}
}
})
},
showActionSheet: function(){
uni.showActionSheet({
itemList: actions,
success:function(res){
console.log(actions[res.tapIndex])
},
fail:function(res){
console.log(res.errMsg)
}
})
}
}
}
</script>
<style>
</style>
顯示:

可以看到,可以對模態彈窗和操作選單進行操作,
總結
uni-app的家口為開發者提供了豐富的功能,包括設備、界面等,我們只需要直接呼叫即可實作所需功能,減少了自己開發的麻煩,有利于快速開發,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/243610.html
標籤:其他
