ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何写一个全局的 Notice 组件?

2022-06-10 13:33:31  阅读:154  来源: 互联网

标签:Notice container rgb 1px props 组件 全局 type


下面将会实现这样的效果:image

  1. 创建Notice组件模板:

    组件模板

    <template>
      <transition
        enter-active-class="animate__animated animate__slideInRight"
        leave-active-class="animate__animated animate__slideOutRight"
        @after-leave="afterLeave"
      >
        <div v-if="isShow" class="notice__root">
          <div :class="`notice-type-${type}`" class="noticer">
            {{
              type === "error"
                ? "&#127827;"
                : type === "success"
                ? "&#127808;"
                : type === "warn"
                ? "&#127819;"
                : "&#128051;"
            }}
            : {{ message }}
          </div>
        </div>
      </transition>
    </template>
    <script>
    export default {
      props: {
        title: {
          type: String,
          default: "",
        },
        message: {
          type: String,
          default: "",
        },
        time: {
          type: Number,
          default: 1000,
        },
        type: {
          type: String,
        },
      },
      data() {
        return {
          isShow: false,
        };
      },
      methods: {
        show() {
          this.isShow = true;
          setTimeout(this.hide, this.time);
        },
        hide() {
          this.isShow = false;
        },
        afterLeave() {
          this.remove();
        },
      },
    };
    </script>
    <style lang="less" scoped>
    @error: rgb(255, 30, 30);
    @warn: rgb(240, 192, 0);
    @success: rgb(0, 144, 74);
    @info: rgb(0, 80, 218);
    
    @errorBg: rgb(255, 208, 208);
    @warnBg: rgb(255, 245, 207);
    @successBg: rgb(210, 255, 233);
    @infoBg: rgb(203, 222, 255);
    
    .notice__root {
      user-select: none;
      padding: 5px 50px 5px 5px;
    }
    
    .noticer {
      padding: 5px 20px;
      margin: 10px 0px;
      // margin-right: 50px;
      border-radius: 8px;
      font-size: 16px;
      width: auto;
      min-width: 280px;
      max-width: 300px;
      word-break: break-all;
      text-align: center;
      box-sizing: border-box;
    }
    .notice-type-error {
      color: @error !important;
      border: 2px solid @error;
      box-shadow: 1px 1px 5px 2px @errorBg;
      background-color: @errorBg;
    
      // border: 1px solid red;
    }
    .notice-type-warn {
      color: @warn !important;
      border: 2px solid @warn;
      background-color: @warnBg;
      box-shadow: 1px 1px 5px 2px @warnBg;
    }
    .notice-type-success {
      color: @success !important;
      border: 2px solid @success;
      background-color: @successBg;
      box-shadow: 1px 1px 5px 2px @successBg;
    }
    .notice-type-info {
      color: @info !important;
      border: 2px solid @info;
      background-color: @infoBg;
      box-shadow: 1px 1px 5px 2px @infoBg;
    }
    </style>
    
  2. 组件动态创建脚本:

    NotificationBanner.js

    import Vue from "vue";
    import Notice from "@/components/Noticer/Notice.vue";
    
    function create(Component, props) {
      // 先建立实例
      const vm = new Vue({
        render(h) {
          //h就是createElement,它返回VNode
          return h(Component, { props });
        },
      }).$mount();
      // 手动挂载
    
      // 判断是否存在container,如果不存在则先创建
      let container;
      container = document.querySelector(".noticer-container");
      if (container == null) {
        container = document.createElement("div");
        container.classList.add("noticer-container");
        container.style.position = "fixed";
        container.style.top = "50px";
        container.style.right = "0px";
        container.style.overflow = "hidden";
        container.style.zIndex = 9999;
    
        document.body.appendChild(container);
      }
    
      container.appendChild(vm.$el);
    
      //销毁方法
      const comp = vm.$children[0];
      comp.remove = function () {
        container.removeChild(comp["$el"]);
        vm.$destroy();
      };
      comp.show();
      return comp;
    }
    
    Vue.prototype.$notice = {
      error: function (props) {
        create(Notice, Object.assign(props, { type: "error" }));
      },
      info: function (props) {
        create(Notice, Object.assign(props, { type: "info" }));
      },
      success: function (props) {
        create(Notice, Object.assign(props, { type: "success" }));
      },
      warn: function (props) {
        create(Notice, Object.assign(props, { type: "warn" }));
      },
    };
    
    
  3. main.js 中引入执行该脚本即可

    import Vue from "vue";
    import App from "./App.vue";
    import "animate.css";
    import "@/components/Noticer/NotificationBanner.js";
    
    
    new Vue({
      render: (h) => h(App),
    }).$mount("#app");
    

标签:Notice,container,rgb,1px,props,组件,全局,type
来源: https://www.cnblogs.com/jaycethanks/p/16362907.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有