【Vue-仿照element-ui自己动手封装ui组件】—— alert、confirm 弹窗

3 篇文章 0 订阅
1 篇文章 0 订阅

使用效果

导入使用

//引入zc-ui组件库
import zcui from "./components/zc-ui"
//注册组件
Vue.use(zcui) 

//使用
this.$alert({
title:"xxx提示标题"//可选项默认为 “提示”
text:"xxx提示文字"//必要项显示提示内容
icon:"info", //可选 弹窗文字旁的图标
confirmText:"确定"//可选确定按钮显示的文本,默认为 “确定”
})

运行效果
效果图

需求分析

1、要能自定义弹窗标题、消息文本,弹窗按钮文本
2、要支持全局的函数式调用,一处引入随从调用
3、弹窗组件要有比较高的权重,覆盖在顶层却不能像普通组件一样受到路由影响
4、弹窗按钮在用户点击后要拿到用户选中的转态,可以通过返回Promise的方式处理

前置知识

1、Vue.extend

在alert和confirm这类弹窗组件中,组件需要动态地创建和销毁,而且最好是像原生组件一样可以随从调用,为此需要用到vue给我们提供的Vue.extend方法以供我们动态生成组件

Vue.extend可与接收一个对象或者一个vue的模板作为参数,执行后返回此模板的构造函数

官方文档例子:https://cn.vuejs.org/v2/api/#Vue-extend

// 创建构造器
var Profile = Vue.extend({
  template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
  data: function () {
    return {
      firstName: 'Walter',
      lastName: 'White',
      alias: 'Heisenberg'
    }
  }
})
// 创建 Profile 实例,并挂载到一个元素上。
new Profile().$mount('#mount-point')
2、Vue.use( )

Vue.use( )官方提供给我们用于插件安装的方法,此方法接收一个插件或方法作为参数,参数为一个函数时函数会直接被执行并把Vue作为参数传入。当传入参数为一个插件时该方法会找到插件下的install方法执行并把Vue作为参数传入,稍后会用到

官方文档:https://cn.vuejs.org/v2/api/#Vue-use

3、import 导入规则

在import导入文件时如果只写目录名,打包时会自动找到目录下的index.js导入,
如:zc-ui/index.js导入时可以写成import zcuifrom "zc-ui"

开始撸代码

文件结构和流程

在这里插入图片描述

代码
alert.vue(样式和结构)

alert.vue

<template>
  <div
    :class="['zc-alert', { backgorundBlur: config.backgorundBlur }]"
    ref="zc_alert"
  >
    <div class="zc-alert-centent">
      <!-- 弹窗标题 -->
      <div class="zc-alert-header">
        {{ config.title || "提示" }}
      </div>
      <!-- 弹窗内容 -->
      <div class="zc-alert-msg">
        <i :class="['iconfont',config.icon]"></i><slot>{{ config.text }}</slot>
      </div>
      <!-- 弹窗按钮 -->
      <div class="zc-alert-btn_box">
        <zc-button v-if="config.type == 'confirm'" class="button" @click="cancel()" >
          	{{ config.cancelText || "取消" }}
          </zc-button
        >
        <zc-button class="button" type="primary" @click="confirm()">
        	{{ config.confirmText || "确定" }}
        </zc-button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "zc-alert",
  props: {
    config: {
      required: true,
    }
  }
};
</script>

<style lang="less" scoped>
.zc-alert {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99999;
  background-color: rgba(0, 0, 0, 0);
  backdrop-filter: blur(0px);
  transition: background-color 0.4s linear;
  perspective: 2000;
  -webkit-perspective: 2000;
  .zc-alert-centent {
    position: relative;
    margin: 0 auto;
    top: 30vh;
    transform: translateY(-30%) rotateX(20deg);
    min-width: 250px;
    max-width: 400px;
    padding: 15px;
    // background-color: rgb(246, 246, 246);
    background-color: #fff;
    // background-image: linear-gradient(180deg,rgb(253, 253, 253),rgb(245, 245, 245));
    border-top: 1px solid #fff;
    border-left: 1px solid #fff;
    border-radius: 5px;
    box-shadow:  5px 10px 10px rgba(0, 0, 0, 0.2),0 20px 30px rgba(0, 0, 0, 0.2);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.17, 0.99, 0.46, 0.99);
    //成功图标
    i.success::before{
      content: "\e644";
      font-size: 16px;
      margin-right: 5px;
      color: #6bce9f;
    }
    //成功图标
    i.info::before{
      content: "\e60b";
      font-size: 16px;
      margin-right: 5px;
      color: #ffa013;
    }
  }
  .zc-alert-header {
    padding: 5px 0 10px;
    font-size: 16px;
    color: rgb(63, 63, 63);
  }
  .zc-alert-msg {
    padding: 5px 0 10px;
    font-size: 14px;
    color: #848484;
  }
  .zc-alert-btn_box {
    display: flex;
    width: 100%;
    justify-content: flex-end;
    margin-top: 10px;
    .button {
      margin-left: 20px;
      height: 30px;
    }
  }
  &.show {
    background-color: rgba(0, 0, 0, 0.5);
    .zc-alert-centent {
      transform: translateY(0) rotateX(0deg);
      opacity: 1;
    }
  }
}
</style>

zc-ui/alert/index.js(组件的事件逻辑处理,给组件添加install方法,导出)

zc-ui/alert/index.js

import alert from './alert.vue'
alert.install = function (Vue) {

    function create(Component, props) {
        return new Promise((resolve, reject) => {
            //方式一:使用Vue.extend创建
            const Ctor = Vue.extend(Component);
            //创建组件实例
            const alertDom = new Ctor({ propsData: { config: props } })
            //挂载
            alertDom.$mount();
            document.body.appendChild(alertDom.$el);//把元素追加到body后面
            setTimeout(() => {
                alertDom.$el.classList.add("show")
            }, 50)
            alertDom.flag = false
            //监听组件的transitionend事件等动画结束后从dom移除并销毁组件
            function handleTransitionend(e) {
                if (alertDom.flag && e.target.classList.contains('zc-alert')) {
                    document.body.removeChild(alertDom.$el) //移除元素
                    alertDom.$destroy() //销毁
                }
            }
            //监听过度动画
            alertDom.$el.addEventListener("transitionend", handleTransitionend)
            alertDom.$el.addEventListener("webkitTransitionEnd", handleTransitionend)
            //移除弹窗组件
            alertDom.remove = () => {
                alertDom.$el.classList.remove("show");
                alertDom.flag = true
            }
            //取消按钮事件处理
            alertDom.cancel = (res) => {
                reject(res)
                alertDom.remove()
            }
            //确定按钮事件处理
            alertDom.confirm = (res) => {
                resolve(res)
                alertDom.remove()
            }
        })
    }
    Vue.prototype.$alert = (obj) => {
        return create(alert, obj)
    }
    Vue.prototype.$confirm = (obj) => {
        return create(alert, { type: 'confirm', ...obj })
    }
};

export default alert
zc-ui/index.js(导入所有组件内,打包导出)

zc-ui/index.js

import alert from './alert'
import button from './button'
//......

const components = [
    alert,
    button,
    //......
]

const install = function (Vue) {
    components.forEach((item)=>{
        Vue.use(item)
    })
}

export default {install}

总结

在其他vue的组件库中其实实现方法都大致雷同,核心都是封装组件模板样式结构,并预留“坑位”,使用时引入并通过Vue.use()把组件注册到全局,使用时用数据把“坑”填上渲染,部分如alerttoast等组件通常会挂载到Vue原型上,使用时通过this.$xxx调用

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在不同的前端框架中添加组件功能的方式和优缺点略有不同: 1. JavaScript:在 JavaScript 中添加组件可以通过原生的 DOM 操作,也可以通过第三方库或框架来实现。原生 DOM 操作需要手动创建、删除和管理 DOM 节点,需要编写大量的代码,维护成本高;而使用第三方库或框架可以提高开发效率,但需要学习和熟悉这些工具的语法和 API。 2. jQuery:jQuery 是一个流行的 JavaScript 库,它封装了原生的 DOM 操作,提供了简洁的语法和强大的功能。使用 jQuery 添加组件可以快速地创建、删除和管理 DOM 节点,同时还提供了丰富的插件和组件库,支持事件处理、动画效果等。然而,jQuery 的语法和 API 过于灵活和简单,可能会导致代码可读性和可维护性降低。 3. VueVue 是一个流行的 MVVM 框架,它提供了组件化的开发方式,可以封装复杂的 UI 组件为可复用的单元,实现模块化开发。Vue组件化开发方式可以提高代码的可维护性和可读性,同时还提供了丰富的生命周期钩子和数据绑定机制,支持组件间通信和事件处理。但是,Vue 的学习曲线较陡,需要掌握其独特的语法和 API,同时还需要熟悉 Vue 的生态系统,如 Vuex 和 Vue Router。 综上所述,不同的前端框架提供了不同的组件化开发方式,开发者需要根据具体的需求和技术水平选择合适的工具和框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值