Vue 通过prototype添加全局属性/方法
通过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()
分类:
Vue
标签:
Vue
, Vue.prototype
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2018-04-26 iOS 获取LaunchImage启动图
2017-04-26 一个简单的算法 - 将一个正整数拆分成指定几个正整数的组合