最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

最简单的vue消息提示全局组件的方法

来源:动视网 责编:小采 时间:2020-11-27 21:55:15
文档

最简单的vue消息提示全局组件的方法

最简单的vue消息提示全局组件的方法:简介 实现功能 自定义文本 自定义类型(默认,消息,成功,警告,危险) 自定义过渡时间 使用vue-cli3.0生成项目 toast全局组件编写 /src/toast/toast.vue <template> <div class=app-toast v-if=isShow
推荐度:
导读最简单的vue消息提示全局组件的方法:简介 实现功能 自定义文本 自定义类型(默认,消息,成功,警告,危险) 自定义过渡时间 使用vue-cli3.0生成项目 toast全局组件编写 /src/toast/toast.vue <template> <div class=app-toast v-if=isShow


简介

实现功能

  • 自定义文本
  • 自定义类型(默认,消息,成功,警告,危险)
  • 自定义过渡时间
  • 使用vue-cli3.0生成项目

    toast全局组件编写

    /src/toast/toast.vue

    <template>
     <div class="app-toast"
     v-if="isShow"
     :class="{'info': type=== 'info','success': type=== 'success','wraning': type=== 'wraning','danger': type=== 'danger'}">{{text}}</div>
    </template>
    <style scoped>
    .app-toast {
     position: fixed;
     left: 50%;
     top: 50%;
     background: #ccc;
     padding: 10px;
     border-radius: 5px;
     transform: translate(-50%, -50%);
     color: #fff;
    }
    .info {
     background: #00aaee;
    }
    .success {
     background: #00ee6b;
    }
    .wraning {
     background: #eea300;
    }
    .danger {
     background: #ee000c;
    }
    </style>
    

    /src/toast/index.js

    import vue from 'vue'
    import toastComponent from './toast.vue'
    
    // 组件构造器,构造出一个 vue组件实例
    const ToastConstructor = vue.extend(toastComponent)
    
    function showToast ({ text, type, duration = 2000 }) {
     const toastDom = new ToastConstructor({
     el: document.createElement('div'),
     data () {
     return {
     isShow: true, // 是否显示
     text: text, // 文本内容
     type: type // 类型
     }
     }
     })
     // 添加节点
     document.body.appendChild(toastDom.$el)
     // 过渡时间
     setTimeout(() => {
     toastDom.isShow = false
     }, duration)
    }
    // 全局注册
    function registryToast () {
     vue.prototype.$toast = showToast
    }
    
    export default registryToast
    

    全局注册

    /main.js

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

    调用

    /src/views/home.vue

    <template>
     <div class="home">
     <input type="button"
     value="显示弹窗"
     @click="showToast">
     </div>
    </template>
    
    <script>
    
    export default {
     name: 'home',
     methods: {
     showToast () {
     this.$toast({
     text: '我是消息'
     // type: 'wraning',
     // duration: 3000
     })
     }
     }
    }
    </script>
    

    文档

    最简单的vue消息提示全局组件的方法

    最简单的vue消息提示全局组件的方法:简介 实现功能 自定义文本 自定义类型(默认,消息,成功,警告,危险) 自定义过渡时间 使用vue-cli3.0生成项目 toast全局组件编写 /src/toast/toast.vue <template> <div class=app-toast v-if=isShow
    推荐度:
    标签: 消息 VUE 全局组件
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top