• vue手写提示组件弹窗


    1、弹框展示

    在这里插入图片描述

    2、message组件

    新建一个message.vue

    <template>
      <div class="wrapper" v-if="isShow" :class="showContent ? 'fadein' : 'fadeout'">{{ text }}</div>
    </template>
    <script>
    
    </script>
    <style scoped>
    .wrapper {
      width: 200px;
      text-align: center;
      position: fixed;
      left: 50%;
      top: 50px;
      background: rgb(237, 242, 252);
      padding: 10px;
      border-radius: 5px;
      border: 1px solid rgb(235, 238, 245);
      transform: translate(-50%, -50%);
      color: rgb(201, 173, 153);
      font-size: 28px !important;
    }
    
    .fadein {
      animation: animate_in 1s;
    }
    
    .fadeout {
      animation: animate_out 1s;
      opacity: 0;
    }
    
    @keyframes animate_in {
      0% {
        opacity: 0;
      }
    
      100% {
        opacity: 1;
      }
    }
    
    @keyframes animate_out {
      0% {
        opacity: 1;
      }
    
      100% {
        opacity: 0;
      }
    }
    </style>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    index.js 跟message组件放在同一目录下

    import vue from 'vue'
    //引入组件
    import messComponent from './message.vue'
    //创建构造器
    const MessConstructor = vue.extend(messComponent)
    function showMessage(text) {
      const message = new MessConstructor({
        el: document.createElement('div'),
        data() {
          return {
            text: text,
            isShow: true,    // 是否显示组件
            showContent: true  // 作用:在隐藏组件之前,显示隐藏动画
          }
        }
      })
      document.body.appendChild(message.$el)
    
    
      setTimeout(() => { message.showContent = false }, 2000)
      // 过了 duration 时间隐藏组件
      setTimeout(() => { message.isShow = false }, 2000 * 2)
    }
    
    
    function messageCom() {
    //挂到vue上去,方便调用
      vue.prototype.$message = showMessage
    }
    export default messageCom
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    main.js 全局引入组件

    import Vue from 'vue'
    import App from './App'
    import router from './router'
    import messageCom from './message/index'
    Vue.config.productionTip = false
    Vue.use(messageCom)
    
    new Vue({
      el: '#app',
      router,
      components: { App },
      template: ''
    })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    调用

    <template>
      <div id="app">
        <input type="button" value="显示提示" @click="showMess">
        <!-- <router-view /> -->
      </div>
    </template>
    
    <script>
    import message from './message/message'
    export default {
      name: 'App',
      components: {  message },
      methods: {
        showMess() {
          this.$message("我是提示消息")
        }
      }
    }
    </script>
    
    <style>
    #app {
      margin-top: 100px;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    redis 学习指南
    mysql主从复制读写分离
    软件过程与建模学习之:Ethics, ACs Code Ethics, IEEE Code of Ethics
    【ONE·C++ || 异常】
    【BIM入门实战】Revit属性对话框中“视图范围”工具的使用方法详解
    基于智能优化算法实现的机械臂避障路径规划(Matlab代码实现)
    浅谈为什么多态只能是指针或引用
    【Spring Boot】034-Spring Boot 整合 JUnit
    【Android笔记30】Android中数据存储之SQLite数据库
    【博学谷学习记录】超强总结,用心分享|Shell运算符
  • 原文地址:https://blog.csdn.net/L221545/article/details/132739764