我封装了一个wx.showModal方法,但是感觉传参有点繁琐,请问有没有好的方式解决传参问题,谢谢🙏
class Tips {
loading() {
wx.showLoading({ mask: true })
}
hide(type, callback, {title, content, confirmText, cancelText, showCancel}) {
wx.hideLoading({
success: (res) => {
if (type === 'Modal') {
this.modal((result) => {
return callback(result)
}, { title, content, confirmText, cancelText, showCancel })
return
};
if (type === 'Toast') {
return this.toast(title)
}
return callback(res)
},
})
}
modal(callback, { title, content, confirmText, cancelText, showCancel }) {
const obj = { title, content, confirmText, cancelText, showCancel };
wx.showModal({
title: obj.title || '提示',
content: obj.content || '提示',
confirmText: obj.confirmText || '确定',
cancelText: obj.cancelText || '取消',
showCancel: obj.showCancel || false,
success: (res) => {
return callback(res)
}
})
}
toast(title) {
wx.showToast({
title: title,
icon: 'none',
duration: 1500,
mask: true,
})
}
};
const show = new Tips();
export default show
调用方法
参数传的顺序不对或不小心漏传就会报错
show.hide('Modal', (res) => {
const eventChannel = this.getOpenerEventChannel()
if (res.confirm) {
const result = this._pickClause();
console.log("result => ", result);
eventChannel.emit('acceptDataFromOpenedPage', { data: result });
wx.navigateBack({
delta: 1,
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}, { content, showCancel });
封装就是为了调用、修改简便,没必要的参数就不要封装进去了