美文网首页
vue简单提示组件

vue简单提示组件

作者: 五洋捉鳖zz | 来源:发表于2019-04-30 14:20 被阅读0次
  • 组件化是最近两年很火的一个话题,几乎每个公司的研发团队在开发的过程中都会或多或少的积累一些组件,无论是定制化的还是非定制的,最早的组件化就是把一个html拆分成几个公共的html(组件)以及一些定制化的html(模块),在这两个部分的基础上开发新的页面会快很多。

  • vue中也比较推荐去写组件。组件的几种调用方式就不赘述了最简单的就是写一个vue文件,在使用的页面import进来,components注册下,然后直接使用就好。今天要说的是直接在vue中通过this就可以调用的组件,类似于elementui中的this.$message.

  • 首先需要简单实现一个简单的提示,背景半透明加中间小的tip提示。

<template>
<transition name="el-fade-in">
  <div class='test-notice' v-if='show'>
    <div class='test-notice-bg'></div>
    <div class='test-notice-content'>
      <div class='icon-area' :class='{"success-icon":type === "success", "error-icon" : type === "error"}'></div>
      <span>{{info}}</span>
    </div>
  </div>
</transition>
</template>
<script>
export default {
  name: 'TestNotice',
  data () {
    return {
      info: '操作成功!',
      type: 'success',
      show: false
    }
  },
  mounted () {
    let _this = this
    _this.show = true
    setTimeout(function () {
      _this.show = false
    }, 2000)
  }
}
</script>
<style lang="scss" scoped>
.test-notice{
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: 3000;
  top: 0px;
  left: 0px;
  .test-notice-bg{
    position: fixed;
    top: 0px;
    left: 0px;;
    width: 100%;
    height: 100%;
    z-index: 1;
    background: #000000;
    opacity: 0.5;
  }
  .test-notice-content{
    width: 440px;
    height: 115px;
    margin: 0 auto;
    background-color: #FFFFFF;
    top: 200px;
    position: relative;
    z-index: 2;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: Center;
    -ms-flex-align: Center;
    align-items: Center;
    .icon-area{
      width: 135px;
      height: 100%;
      position: absolute;
      top: 0px;
      left: 0px;
    }
    .error-icon{
      background:linear-gradient(90deg,rgba(229,57,53,1),rgb(223, 163, 162));
      &::before{
        content: '';
        width: 50px;
        height: 50px;
        position: absolute;
        top: 32.5px;
        left: 42.5px;
        background: url(/static/images/admin/error.png) center center no-repeat;
        background-size: contain;
      }
    }
    .success-icon{
      background:linear-gradient(90deg,rgba(26,180,0,1),rgba(29,199,1,1));
      &::before{
        content: '';
        width: 50px;
        height: 50px;
        position: absolute;
        top: 32.5px;
        left:42.5px;
        background: url(/static/images/admin/success.png) center center no-repeat;
        background-size: contain;
      }
    }
    span{
      float: left;
      position: relative;
      font-size:22px;
      font-family:MicrosoftYaHei;
      font-weight:400;
      color:rgba(51,51,51,1);
      padding-left: 20px;
      width: calc(100% - 135px);
      margin-left: 135px;
    }
  }
}
</style>

  • 在组件的统计目录新建一个index.js文件
  import vue from 'vue'

import TestNotice from './TestNotice.vue'
const NoticeConstructor = vue.extend(TestNotice )
function showNotice (info, type) {
  // 实例化一个 toast.vue
  const noticeDom = new NoticeConstructor({
    el: document.createElement('div'),
    data () {
      return {
        info: info,
        type: type
      }
    }
  })

  // 把 实例化的 toast.vue 添加到 body 里
  document.body.appendChild(noticeDom.$el)
}
function registryNotice () {
  vue.prototype.$TestNotice = showNotice 
}

export default registryNotice

  • 大功告成~,剩下的就是需要在main.js中引入下就ok了
// In main.js
import KtNotice from '@/components/notice/index'
Vue.use(KtNotice)
//In SomeVue.vue
...
 methods:{
   yourMethods () {
        this.$TestNotice('我出来啦,我是国产小提示!')
   }
}
...

相关文章

  • vue简单提示组件

    组件化是最近两年很火的一个话题,几乎每个公司的研发团队在开发的过程中都会或多或少的积累一些组件,无论是定制化的还是...

  • 10.实战 4:全局提示组件——$Alert

    实战 4:全局提示组件——$Alert 有一种 Vue.js 组件,它不同于常规的组件,但组件结构本身很简单,比如...

  • 子组件向父组件传值,自定义事件

    需求:描述:在主页点击购物车弹出提示弹窗父组件:index.vue子组件:modal.vue 代码:modal.v...

  • 初学vue

    Vue通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件; 一个简单vue实例 组件化 组件(Compo...

  • vue-2.0 loading 组件

    参考 vue-hackernews-2.0 项目里面的 spinner.vue 组件,做了简单的修改。修改后的组件...

  • vue学习:webpack+vue(组件学习)

    这篇文章主要简单的讲一下vue组件: -1- vue 组件分为全局组件和局部组件,我们先来说一下全局组件: 这里我...

  • Vue组件命名的一些问题

    原文:聊聊 Vue 组件命名那些事 组件注册 我们以一个最简单的例子来研究 Vue 组件的注册过程: 通过跟踪代码...

  • Vue+Vuex 实现全局统一错误提示

    背景:最近做的Vue项目里需要实现全局统一错误提示组件 之前的错误提示统一放在axios封装的组件里,因为请求的时...

  • vue tree demo 数组件

    vue-tree-demo 一个vue 递归组件时间的 树demo 简单介绍 各大UI组件库 基本都有自己tree...

  • vue组件化案例

    简单的vue 组件化demo,效果很简单,就是一个列表增删改查。 未组件化代码: 效果: 组件化后: 效果: 详细...

网友评论

      本文标题:vue简单提示组件

      本文链接:https://www.haomeiwen.com/subject/qoxvnqtx.html