Home > Web Front-end > JS Tutorial > body text

How to use global prompt box component in vue?

亚连
Release: 2018-06-04 14:20:22
Original
2517 people have browsed it

This article mainly introduces the example code of vue's global prompt box component. Friends who need it can refer to it

This article introduces you to a vue global prompt box component. The specific code is as follows:

<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>
Copy after login

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
Copy after login

How to use?

In main.js

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

In component

this.$toast("XXXXXXXXX");
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

About the method of closing the component by clicking outside the component in Vue (detailed tutorial)

Details for you Solve the problem of white screen on the homepage after vue build is packaged (detailed tutorial)

About the method of not displaying router-view in routing in vue2.0 (detailed tutorial)

The above is the detailed content of How to use global prompt box component in vue?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!