jy-safe-app/components/custom/show-info/initToast.js

46 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2025-10-14 15:17:30 +08:00
import Vuex from 'vuex'
export default function initToast(v) {
// 挂在store到全局Vue原型上
v.prototype.$toastStore = new Vuex.Store({
state: {
show: false,
icon: "success", //success:成功fail:失败
title: "标题",
content: '内容',
success: null,
},
mutations: {
hideToast(state) {
// 小程序导航条页面控制
// #ifndef H5
if (state.hideTabBar) {
wx.showTabBar();
}
// #endif
state.show = false
},
showToast(state, data) {
state = Object.assign(state, data)
state.show = true
setTimeout(() => {
state.show = false
return state.success(state.icon)
}, 2000)
},
}
})
// 注册$showToast到Vue原型上以方便全局调用
v.prototype.$showToast = function(option) {
if (typeof option === 'object') {
// #ifndef H5
if (option.hideTabBar) {
wx.hideTabBar();
}
// #endif
v.prototype.$toastStore.commit('showToast', option)
} else {
throw "配置项必须为对象传入的值为:" + typeof option;
}
}
}