通过prototype定义Vue实例的原型属性,使每个Vue实例中都可以用,又不会污染全局作用域。
toast.js
const Toast = {
//设置缓存(expire为缓存时效)
show(title, icon = 'none', duration = 1500) {
uni.showToast({
title: title || '',
icon: icon,
duration: duration
})
},
showLoading(title) {
uni.showLoading({
title: title || ''
})
},
hideLoading() {
uni.hideLoading()
}
}
export default Toast;
main.js
import toast from 'utils/toast.js'
Vue.prototype$toast = toast
使用时通过this.$toast可以直接取出该toast对象
this.$toast.showLoading()
this.$toast.hideLoading()
如果不想每次都输$可以通过
//注册
Vue.prototype['toast'] = toast
//访问的时候
this.toast.showLoading()
this.toast.hideLoading()