uniapp 自定义showToast样式

自定义一个uniapp的showToast样式

效果图

 

在组件里新建一个文档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/images/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">{{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: 238.5px;
			  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>

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;
	}
  }
}

在main.js中引入

import initToast from "@/components/show-toast/initToast.js"
import showToast from "@/components/show-toast/show-toast.vue"

Vue.component('show-toast',showToast)

initToast(Vue);

在父组件中使用

vue代码中在想要使用的地方使用该组件

<show-toast></show-toast>

js代码中

this.$showToast({
    title:"提交成功,感谢您的反馈!",
	content:"",
	icon:'success',
	success:res=>{
		console.log("[dsdhfdhsh]")
	}
});

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
uniapp中,可以使用uni.showToast()方法来显示一个toast提示框,但是它的样式是默认的,如果需要自定义样式,可以通过在App.vue中注册一个全局组件来实现。 具体步骤如下: 1. 在App.vue中注册一个全局组件,例如Toast。 ``` <template> <div class="toast-container" :class="{'show': visible}"> <div class="toast" :class="[type]"> {{ message }} </div> </div> </template> <script> export default { data() { return { visible: false, message: '', type: '' } }, methods: { show(options) { this.message = options.message || '' this.type = options.type || '' this.visible = true setTimeout(() => { this.visible = false }, options.duration || 2000) } } } </script> <style scoped> .toast-container { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 9999; } .toast { display: inline-block; padding: 10px 20px; border-radius: 4px; font-size: 14px; color: #fff; text-align: center; background-color: rgba(0, 0, 0, 0.7); } .toast.success { background-color: #52c41a; } .toast.warning { background-color: #faad14; } .toast.error { background-color: #f5222d; } .toast.show { opacity: 1; transition: opacity 0.3s; } .toast.hide { opacity: 0; transition: opacity 0.3s; } </style> ``` 2. 在需要显示toast的页面中,通过this.$refs.toast.show()方法来调用Toast组件。 ``` <template> <view> <button @click="showToast">显示toast</button> </view> </template> <script> export default { methods: { showToast() { this.$refs.toast.show({ message: '这是一个toast提示', type: 'success', // success/warning/error duration: 3000 // 可选,单位为毫秒,默认为2000 }) } } } </script> <style> </style> ``` 以上就是在uniapp自定义toast提示框样式的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值