Toast模块的封装

注:对组件进行全局封装,省去每次都需要import和注册组件的麻烦

1 Html

//用于显示弹出提示信息
<div v-show="isShow" class="Toast">
	<div>{{message}}</div>
</div>

2 js

export default{
	name:'Toast',
	data(){
		return{
			message:'',
			isShow:false
		}
	},
	methods:{
		show(message,duration){
			this.isShow=true
			this.message=message
			setTimeout(()=>{
				this.isShow=false
				this.message=''
			},duration)
		}
	}
}

3 CSS

//水平垂直居中,面试必考,不会的赶紧复习
.toast{
	position:fixed;
	left:50%;
	top:50%;
	transform:translate(-50%,-50%);
	z-index:999;   //防止在最下面
	background-color:rgba(0,0,0,0.6);
	color:#fff;
}

4 封装

import Toast from './Toast.vue'

const obj={}

obj.install=funciton(Vue){
	//创建组件构造器
	const ToastConstructor=Vue.extend(Toast)
	//实例化组件构造器对象
	const toast=new ToastConstructor()
	//将组件对象手动挂载
	toast.$mount(document.creatElement('div'))
	//添加到body中
	document.body.appendChild(toast.$el)
	//添加到Vue原型上
	Vue.prototype.$toast=toast
}


export default obj

5 在main.js中使用

import toast from './index.js'

Vue.use(toast)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值