使用Api的形式封装Vue组件(一)

编写一个右下角弹出提示的vue插件,以api的形式调用。

从构建出基础代码,计算出显示的位置;添加自动消失;消失后删除dom,vm对象;生成多个,删除某个,重新计算位置;鼠标聚焦不关闭,移出重新开始自动消失。

一步一步完善,优化功能。

在文件夹notification下新增一个notification.vue文件 编写基础代码

<template>
  <transition name="fade">
    <div class="notification">
      <span class="container">{{content}}</span>
      <a class="btn" @click.stop.prevent="handleClose">{{btn}}</a>
    </div>
  </transition>
</template>
<script>
  export default {
    name: 'notification',
    props: {
      content: {
        type: String,
        require: true
      },
      btn: {
        type: String,
        default: '关闭'
      }
    },
    methods: {
      handleClose (e) {
        this.$emit('close')
      }
    }
  }
</script>

在同级目录下新建index.js

import Notification from './notification.vue'
export default (Vue) => {
  Vue.component(Notification.name, Notification)
}

在vue入口文件的index.js增加

import Notification from './components/notification'
Vue.use(Notification)
之后在页面的vue文件中增加
<notification content="test notification"></notification>
至此 组件初步搭建完成



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值