手把手教你撸vue全局组件

本文详细介绍如何在Vue项目中创建自定义的Alert警告组件,包括组件的开发过程、样式设置及在不同场景下的注册与使用方法。

在开发vue项目时,经常会用到element UI组件,在全局使用像message,toast,alert等组件得时候会很方便,但是在开发需要UI制定的项目时,就不能使用element UI了,这时就需要自己去开发属于自己的组件库了。 具体的开发方法官网提供了。下面我们就根据官网提供的方法来开发属于我们自己的alert警告组件

  1. 首先我们要在src/components文件夹下创建一个alert文件夹,在里面创建两个文件
├── src
│   ├── components
│   │   ├── alert
│   │   │   ├── alert.vue #alert组件
│   │   │   ├── index.js #插件逻辑
复制代码
  1. 编写alert组件
<template>
  <transition name="alert-fade">
    <div
      class="ht-message"
      v-if="show"
    >
      <div
        class="ht-model"
        @click="close"
      ></div>
      <div
        class="ht-message-box"
        :style="'width:'+width"
      >
        <div class="ht-message-header">
          <i
            class="iconfont icon-error ht-message-close"
            @click="close"
          ></i>
        </div>
        <div class="ht-message-body">
          {{message}}
        </div>
        <div class="ht-message-footer">
          <slot name="footer">
            <div
              class="ht-message-btn"
              @click="close"
            >
              确定
            </div>
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  export default {
    name: 'Alert',
    props: {
      width: {
        //弹框的宽度
        type: String,
        required: false,
        default: '30%'
      }
    },
    data() {
      return {
        show: false
      }
    },
    methods: {
      close() {
        this.show = false
        this.$el.remove() //再点击关闭得时候要将其移出,(仅在第一种方法中使用)
        this.confirmSure()
      }
    }
  }
</script>

<style scoped lang="less">
  .alert-fade-enter-active,
  .alert-fade-leave-active {
    transition: opacity 0.3s;
  }
  .alert-fade-enter,
  .alert-fade-leave-to {
    opacity: 0;
  }
  .ht-message {
    .ht-model {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 500;
      background: rgba(0, 0, 0, 0.5);
    }
    .ht-message-box {
      position: fixed;
      top: 50%;
      left: 50%;
      z-index: 501;
      background-color: #fff;
      -webkit-transform: translate(-50%, -50%);
      -moz-transform: translate(-50%, -50%);
      -ms-transform: translate(-50%, -50%);
      -o-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      border-radius: 10px;
      padding: 5px 10px;
      .ht-message-body {
        text-align: center;
        font-size: 18px;
        color: #333;
      }
      .ht-message-header {
        height: 30px;
        .ht-message-close {
          float: right;
          padding: 5px 10px;
          margin: -5px -10px;
          cursor: pointer;
          color: #666;
        }
      }
      .ht-message-footer {
        width: 100%;
        .ht-message-btn {
          margin: 40px auto 30px;
          padding: 0 30px;
          width: 200px;
          height: 40px;
          font-size: 18px;
          color: #fff;
          background-color: #ff3f47;
          -webkit-border-radius: 20px;
          -moz-border-radius: 20px;
          border-radius: 20px;
          line-height: 40px;
          text-align: center;
          cursor: pointer;
        }
      }
    }
  }
</style>

复制代码
  1. 现在就要index.js中注册组件并完成业务逻辑了,(==在这里我们要注意,如果我们要在除了vue组件以外的地方使用得话,例如我们封装的api.js里如果需要alert某些请求状态的时候,需要下面第一种写法==)
import AlertComponent from './alert.vue'
import Vue from 'vue'
const Alert = (msg, confirmSure = () => {}) => {
  const Constructor = Vue.extend(AlertComponent) //创建一个alert子实例
  let instance = new Constructor({
    el: document.createElement('div'), //将alert实例挂载到创建的div上
    data() {
      return {
        message: msg, //需要显示的信息
        show: true //在调用alert时显示组件
      }
    },
    methods: {
      confirmSure: confirmSure //点击关闭的时候触发的回调函数
    }
  })
  document.body.appendChild(instance.$el) //添加到body中
}

export default Alert

复制代码

但是如果我们只是在vue组件中使用的话,使用下面第二种注册方法

import AlertComponent from './alert.vue'

const Alert = {
  install(Vue) {
    const Constructor = Vue.extend(AlertComponent) //创建一个alert子实例
    let instance = new Constructor({
      el: document.createElement('div') //将alert实例挂载到创建的div上
    })
    document.body.appendChild(instance.$el) //添加到body中
    //绑定到vue原型上,以供全局使用
    Vue.prototype.$alert = (msg, confirmSure = () => {}) => {
      instance.message = msg //需要显示的信息
      instance.show = true //在调用alert时显示组件
      instance.confirmSure = confirmSure //点击关闭的时候触发的回调函数
    }
  }
}

export default Alert

复制代码
  1. 最后我们在main.js引入的时候也需要注意,如果我们要在组件和组件以外的情况下使用alert的话,我们需要这样引入
import Alert from '@/components/alert'
Vue.prototype.$alert = Alert
复制代码

再如果我们只是在vue组件里使用的话,我们要这样引入

import Alert from '@/components/alert'
Vue.use(Alert)
复制代码
  1. 最后我们就可以愉快的调用组件了
//调用方法(组件以外的调用方式)
import Alert from '@/components/alert'
Alert('用户会话过期,请重新登录', () => {
    console.log('我被调用了')
})
组件内使用
this.$alert('用户会话过期,请重新登录', () => {
    console.log('我被调用了')
})
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值