uni-app封裝toast和loading
阿新 • • 發佈:2021-01-29
在common下新建fun.js
// 不含icon提示框
const toast = str => {
return new Promise((resolve, reject) => {
uni.showToast({
title: str,
icon: "none",
duration: 2000,
success: () => {
setTimeout(() => {
resolve
}, 2000)
}
})
} )
};
// 成功提示框
const successToast = str => {
return new Promise((resolve, reject) => {
uni.showToast({
title: str,
icon: "success",
duration: 2000,
success: () => {
setTimeout(() => {
resolve()
}, 2000)
}
})
})
};
// loading
const showLoading = () => {
return new Promise((resolve, reject) => {
uni.showLoading({
success: () => {
resolve()
}
})
})
};
// tipLoading ==>提示loading
const tipLoading = str => {
return new Promise((resolve, reject) => {
uni.showLoading({
title: str,
success: ( ) => {
resolve()
}
})
})
};
// 隱藏loading
const hideLoading = () => {
return new Promise((resolve, reject) => {
uni.hideLoading({
success: () => {
resolve()
}
})
})
};
export default {
toast: toast,
successToast: successToast,
showLoading: showLoading,
tipLoading: tipLoading,
hideLoading: hideLoading,
}
在main.js中全域性引入並使用
import $ from 'common/fun.js'
Vue.prototype.$ = $
在頁面呼叫
t.$.toast("提示訊息");
t.$.successToast('成功提示訊息');
t.$.showLoading();
t.$.tipLiading("提示loading");
t.$.hideLoading();