温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Vue中自定义一个toast组件

发布时间:2021-06-11 17:53:25 来源:亿速云 阅读:216 作者:Leah 栏目:web开发

本篇文章为大家展示了如何在Vue中自定义一个toast组件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

第一步:写toast.vue,将样式之类的先定下来

<template>
 <div v-show="showToast" class="toast" :class="position">
 <div class="toast_container" v-if="type=='success'">
  <div><i class="iconfont icon-check icon"></i></div>
  <div class="msg_container">{{message}}</div>
 </div>
 <div class="toast_container" v-else-if="type=='wrong'">
  <div><i class="iconfont icon-warning-circle icon"></i></div>
  <div class="msg_container">{{message}}</div>
 </div>
 <div class="toast_container" v-else-if="type=='loading'">
  <div><loading10></loading10></div>
  <div class="msg_container">{{message}}</div>
 </div>
</div>
</template>
<script>
import loading10 from '../loading/spiner'
export default{
 props:{
  message:String,
  type:{
   validator: function (value) {
   // 值必须是这些字符串中的一个
   return ['success', 'wrong', 'loading'].indexOf(value) !== -1
  },
   default:'success'
  },
  duration:{
   type:Number,
   default:3000
  },
   position:{
   type:String,
   default:'middle'
  }
 },
 components:{
  loading10
 },
 data(){
  return{
   showToast:false
  }
 }
}
</script>
<style scoped>
.toast{
 width:100%;
}
.toast_container{
 background: rgba(0, 0, 0, 0.7);
 border-radius: 8px;
 color:#fff;
 margin-left:88px;
 margin-right:88px;
 text-align:center;
 padding-top:15px;
 padding-bottom: 15px; 
}
.top{
 position:absolute;
 top:10%;
}
.middle{
 position:absolute;
 top:40%;
}
.bottom{
 position:absolute;
 top:70%;
}
.msg_container{
 margin-top:8px;
 margin-left:15px;
 margin-right:15px;
 line-height: 22px;
 font-size: 16px;
 word-wrap: break-word;
}
.icon{
 font-size:30px;
}
</style>

如何在Vue中自定义一个toast组件如何在Vue中自定义一个toast组件如何在Vue中自定义一个toast组件

一共三种样式,成功(success),失败(wrong),加载中(loading);

一共三种位置,上(top),中(middle),下(bottom);

所有涉及的图案出自阿里的iconfont 手机淘宝图标库。

加载中动画是自己写的蹩脚的加载组件(emmm,就不放出来污染大家眼睛了,需要的可以评论区知会一声_(:з」∠)_)

第二步:写index.js ,完成toast组件的实例化

import Vue from 'vue'
import Toast from './toast'
let singleToast=true;
let queue=[];
function createInstance(){
 // 返回一个扩展实例构造器
 if(!queue.length||!singleToast){
 const ToastConstructor = Vue.extend(Toast);
 // 构造一个实例
 const toastDom = new ToastConstructor({
 el: document.createElement('div'),
 });
 // 把实例化的 toast.vue 添加到 body 里
 document.body.appendChild(toastDom.$el);
 queue.push(toastDom);
 singleToast=true;
 return toastDom;
 }
};
// 注册为全局组件的函数
function toast(options= {}) {
 const toastDom = createInstance();
 toastDom.message =typeof options === 'string' ? options : options.message;
 toastDom.type = options.type || 'success';
 toastDom.duration = options.duration || 3000;
 toastDom.position = options.position || 'middle';
 if(!toastDom.message){
 toastDom.showToast =singleToast= false;
 }else{
 toastDom.showToast=true;
 setTimeout(() => {toastDom.showToast =singleToast= false} ,toastDom.duration);
 }
}
// 将组件注册到 vue 的 原型链里去,
// 这样就可以在所有 vue 的实例里面使用 this.$toast()
// Vue.prototype.$toast = showToast
Vue.prototype.$toast = toast;
export default toast

设置singleToast和queue的目的在于:确保同一时期界面上只有一个toast,不能同时出现多个toast。

由于toast会初始化,因此为了避免在任何操作之前界面上就出现一个toast,用if语句判断:

如果没有传入的message,则不显示toast(这样可以使得初始化的toast不显示)

否则显示,并且过一定时间消失,只有singleToast为false,说明此刻界面上没有toast,才能再新建一个toast实例(因为此时if判断内queue.length 不为0【初始化的toast组件本身占了一个位置】,而singleToast为false,因此可以创建)

第三步:使用

在main.js 添加如下代码:

import toast from './components/toast/index'
Vue.use(toast)

创建需要调用的Vue文件:

<template>
 <div>
  <input type="button" value="显示弹窗" @click="showToast">
 </div>
</template>
 <script>
 export default {
  methods: {
   showToast () {
    this.$toast({message:'加载中',type:'loading',position:'bottom',
     duration:'2000'});
    // this.$toast('成功提示');
   }
  }
 }
 </script>

如何在Vue中自定义一个toast组件

上述内容就是如何在Vue中自定义一个toast组件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI