vue 组件全局封装_关于vue中全局组件通用组件的封装及使用方法介绍

本文介绍了如何在Vue中封装全局组件,包括Notify消息提示组件和Loading加载组件。通过创建单例模式,使得组件在项目任意地方可直接调用。详细讲解了组件的实现与注册过程,提供了一种提高开发效率的方法。
摘要由CSDN通过智能技术生成

使用vue开发项目,有些UI

vue使用install定义安装全局组件需要install和use这两个api来配合才能完成,组件通过export暴露出install方法 。

但如果直接将指定组件赋值到vue的prototype上,组件不需要vue.use安装直接就能用。

1.全局组件单例模式

这里通过实现一个 Notify移动端消息提示组件和加载组件,来对组件的封装过程进行详述。

实例一: Notify组件的封装

Notify.vue

{ { message }}

export default {

name: "Notify",

data() {

return {

show1: false,

$_timer: null,

className: "fadein",

time: 2000

};

},

props: {

message: {

type: String,

required:true,

default: "提示信息"

},

visible: {

type: Boolean,

default: false

},

duration: {

type: Number,

default: 2000

},

type: {

type: String,

default: "default"

}

},

mounted() {},

methods: {

fire() {

const _this = this;

if (_this.$_timer) {

clearTimeout(_this.$_timer);

}

if (_this.visible) {

if (_this.duration) {

_this.time = _this.duration;

}

_this.$_timer = setTimeout(() => {

_this.hide();

}, _this.time);

}

},

show() {

this.className = "fad

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值