【无标题】

在components内写

initToast.js

复制代码
import Vuex from 'vuex'
export default function initToast(v) {
  v.use(Vuex)
  // 挂在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)
			console.log(state);
			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;
	}
  }
}

show-toast.vue

复制代码
<template>
	<view class="_showToast" v-show="show">
		<view class="_shade"></view>
		<view class="_ToastBox">
			<view class="Toast-box">
				<view style="height: 10px;"></view>
				<image v-if="icon=='success'" class="Toast-icon" src="@/static/image/success.png"></image>
				<text v-if="icon=='success'" class="Toast-title-success">{{title}}</text>
				<!-- <image v-if="icon=='fail'" class="Toast-icon" src="@/static/fail.png"></image> -->
				<text v-if="icon=='fail'" class="Toast-title-fail">{{title}}</text>
				<text class="Toast-subtitle" style="white-space: nowrap;">{{content}}</text>
			</view>
		</view>	
	</view>
</template>
 
<script>
	export default {
		name:"show-toast",
		data() {
			return {
				
			};
		},
		computed: {
			show(){
				return this.$toastStore.state.show;
			},
			title(){
				return this.$toastStore.state.title;
			},
			content(){
				return this.$toastStore.state.content;
			},
			icon(){
				return this.$toastStore.state.icon;
			}	
		},
		mounted() {
			setTimeout(()=>{
				this.$toastStore.commit('hideToast')
				this.$toastStore.commit('success',"confirm")
			},3000)
		},
		methods:{
			closeToast(){
				this.$toastStore.commit('hideToast')
			},
			clickBtn(res){
				this.$toastStore.commit('hideToast')
				this.$toastStore.commit('success',res)
			}
		},
		beforeDestroy(){
			this.$toastStore.commit('hideToast')
		},
	}
</script>
 
<style lang="scss" scoped>
._showToast{
	position: fixed;
	top: 0;
	left:0;
	width: 100%;
	height: 100%;
	z-index:10000;
	._shade{
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		background: #000;
		opacity: .6;
		z-index:11000;
	}
	._ToastBox{
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		z-index:12000;
		display: flex;
		justify-content: center;
		align-items: center;
		.Toast-box{
			  position: absolute;
			  width: 300px;
			  min-height: 169.75px;
			  top:50%;
			  left: 50%;
			  transform: translate(-50%,-50%);
			  background: #FFFFFF;
			  box-shadow: 0px 10px 20px 0px rgba(28, 23, 47, 0.2);
			  border-radius: 14px;
			  display: flex;
			  flex-direction: column;
			  align-items: center;
			  .Toast-icon{
				  width: 71px;
				  height: 71px;
				  display: block;
				  margin-top:17px;
			  }
			  .Toast-title-fail{
				  font-size: 20px;
				  font-family: Source Han Sans CN;
				  font-weight: bold;
				  color: #EC4E4E;
				  margin-top: 18px;
			  }
			  .Toast-title-success{
				  font-size: 14px;
				  font-family: Source Han Sans CN;
				  // font-weight: bold;
				  color: #40565C;
				  margin-top: 18px;
			  }
			  .Toast-subtitle{
				  font-size: 17px;
				  font-family: Source Han Sans CN;
				  font-weight: 400;
				  color: #666666;
				  margin-top: 6px;
				  padding: 0 12px 12px 12px;
			  }
		}
	}				
}	
</style>

项目中引用

复制代码
<template>
<show-toast></show-toast>
</template>




this.$showToast({
				title:"提交成功",
				content:"感谢您对学校安全管理工作做出的贡献!",
				icon: 'success',
				success: (res) => {
			    // console.log("[dsdhfdhsh]")
								}
							});
相关推荐
老毛肚15 分钟前
jeecg-boot-base-core 02 day
javascript·python
yaoxin52112321 分钟前
434. Java 日期时间 API - Period 基于日期的时间段
java·开发语言·python
凡人叶枫1 小时前
Effective C++ 条款30:透彻了解 inlining 的里里外外
linux·开发语言·c++·嵌入式开发·effective c++
学逆向的1 小时前
C++纯虚函数
开发语言·c++·网络安全
程序员二叉2 小时前
【JUC】ThreadLocal底层原理|内存泄漏|弱引用|跨线程传递方案
java·开发语言·面试·职场和发展·juc
程序员二叉2 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
凡人叶枫2 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
Qt程序员2 小时前
掌握 Linux 内核调度:从原理到实现(进程篇)
java·开发语言
code bean3 小时前
【LangChain】检索器完全指南:从向量检索到生产级 RAG 架构
java·开发语言·微服务
LabVIEW开发3 小时前
LabVIEW + MATLAB 混合编程:爆炸场测试数据精准采集方案
开发语言·matlab·labview