首页  >  文章  >  web前端  >  在vue中如何使用全局提示框组件?

在vue中如何使用全局提示框组件?

亚连
亚连原创
2018-06-04 14:20:222487浏览

这篇文章主要介绍了vue的全局提示框组件实例代码,需要的朋友可以参考下

这篇文章给大家介绍一个vue全局提示框组件,具体代码如下所示:

<template>
   <!-- 全局提示框 -->
   <p v-show="visible" class="dialog-tips dialog-center">
     <p>{{message}}</p>
   </p>
</template>
<script>
export default {
 data() {
  return {
   visible: false,
   message: ""
  };
 }
};
</script>
<style lang="scss">
.dialog-tips{
  position: fixed;
  z-index: 100;
  min-width: 220px;
  padding: 40px 22px;
  white-space: nowrap;
  background-color: #fff;
  box-shadow: 0px 8px 15px 0 rgba(0, 0, 0, 0.1);
  text-align: center;
  .dialog-tips-icon{
    width: 54px;
    height: 54px;
    @extend %bg-contain;
    display: inline-block;
    margin-bottom: 13px;
  }
}
.dialog-center {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%)
}
</style>

toast.js

import ToastComponent from &#39;./toast.vue&#39;
const Toast = {};
// 注册Toast
Toast.install = function (Vue) {
  // 生成一个Vue的子类
  // 同时这个子类也就是组件
  const ToastConstructor = Vue.extend(ToastComponent)
  // 生成一个该子类的实例
  const instance = new ToastConstructor();
  // 将这个实例挂载在我创建的p上
  // 并将此p加入全局挂载点内部
  instance.$mount(document.createElement(&#39;p&#39;))
  document.body.appendChild(instance.$el)
  // 通过Vue的原型注册一个方法
  // 让所有实例共享这个方法 
  Vue.prototype.$toast = (msg, duration = 1500) => {
    instance.message = msg;
    instance.visible = true;
    setTimeout(() => {
      instance.visible = false;
    }, duration);
  }
}
export default Toast

如何使用?

  在main.js中

 import Vue from &#39;vue&#39;
  import Toast from &#39;./toast&#39; 
  Vue.use(Toast);

  在component中

this.$toast("XXXXXXXXX");

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

有关在Vue中点击组件外关闭组件方法(详细教程)

详细为你解决vue build打包之后首页白屏的问题(详细教程)

有关在vue2.0中路由不显示router-view方法(详细教程)

以上是在vue中如何使用全局提示框组件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn