封装吐司toast插件

普通封装:

Toast.vue:

		<template>
		  <div class="toast">
		    {{msg}}
		  </div>
		</template>
		<script>
		  export default {
		    name: 'Toast',
		    props: {
		      msg: String
		    }
		  }
		</script>
		<style scoped>
		  .toast {
		    padding: 15px 20px;
		    background-color: rgba(0, 0, 0, .5);
		    border-radius: 10%;
		    color: #fff;
		    position: fixed;
		    left: 50%;
		    top: 50%;
		    transform: translate(-50%, -50%);
		  }
		</style>
使用:
		引入 import Toast from '...'
		<Toast :msg="msg"
		       v-show="isShowToast"/>
		//加入购物车
		addCar() {
		  this.msg = 'hello 你好啊'
		  this.isShowToast = true;
		  setTimeout(() => {
		    this.isShowToast = false;
		    this.msg = '';
		  }, 300);

插件方式封装:

首先和Toast.vue同级,在toast文件夹下新建一个index文件,
然后在main文件中导入此js文件,并且使用Vue.use(Toast)安装

当执行 Vue.use() 时,就会执行install 函数,
利用此方式,在index 的install方法中设置

 --main.js: 

            import Toast from '...'

            Vue.use(Toast);

-- index.js:

			const obj = {};
			import Toast from './Toast.vue'

			obj.install = function (Vue) { //默认有Vue参数
			  const vueConstructor = Vue.extend(Toast);
			  const toast = new vueConstructor();
			  toast.$mount(document.createElement('div'));
			  document.body.appendChild(toast.$el);
			  Vue.prototype.$toast = toast;
			};
			export default obj;
--Toast.vue:

			<template>
			  <div class="toast" v-show="isShowToast">
			    {{msg}}
			  </div>
			</template>
			<script>
			  export default {
			    name: 'Toast',
			    data: function () {
			      return {
			        msg: '',
			        isShowToast: false
			      }
			    },
			    methods: {
			      show(msg, duration) {
			        duration = duration || 500;
			        this.msg = msg;
			        this.isShowToast = true;
			        setTimeout(() => {
			          this.isShowToast = false;
			          this.msg = '';
			        }, duration);
			
			      }
			    }
			  }
			
			</script>
			<style scoped>
			  .toast {
			    position: fixed;
			    background-color: rgba(0, 0, 0, .6);
			    padding: 10px 15px;
			    color: #fff;
			    left: 50%;
			    top: 50%;
			    transform: translate(-50%, -50%);
			  }
			</style>
---使用:
			this.$toast.show( ' 内容 ', 时间)
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值