vue自定义全局弹出询问框、输入框、提示框、toast(附源码)

这篇具有很好参考价值的文章主要介绍了vue自定义全局弹出询问框、输入框、提示框、toast(附源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

早前写过一篇关于vue自定义弹出询问框、输入框、提示框的贴子,当时只是实现了组件化,组件需要在各个业务模板进行引用,不能全局化使用,不太方便,本次将其改进成了全局使用,具体的业务模块不需要引入组件,直接调用main.js注册的全局方法即可。

涉及技术点:

遮罩层样式,自定义组件,子组件套用组件,子组件调用父组件方法,组件属性监听,输入框默认获得焦点,输入框数据双向绑定、组件注册

效果图如下:

询问框:

vue提示弹窗,vue.js,vue.js,javascript,前端,Powered by 金山文档

输入框:

vue提示弹窗,vue.js,vue.js,javascript,前端,Powered by 金山文档

提示框

vue提示弹窗,vue.js,vue.js,javascript,前端,Powered by 金山文档

Toast

vue提示弹窗,vue.js,vue.js,javascript,前端,Powered by 金山文档

询问框组件代码

<template>
  <div v-if="show" class="mask">
    <div class="dlg-msg-box flex flex-col">
      <div class="flex flex-space-between full-width">
        <div class="font-bold">{{caption}}</div>
        <div class="pointer" @click="close"><img class="logo-22" src="@/assets/images/guanbi.png"/></div>
      </div>
      <div class="margin-top-l" style="height:45px;">{{msg}}</div>
      <div class="flex flex-end margin-top-xl">
        <div class="btn-huibai-auto pointer" style="width:80px" @click="cancelClick">取消</div>
        <div class="margin-left-m btn-blue-auto pointer" style="width:80px" @click="confirmClick">确定</div>
      </div>
    </div>
  </div>
</template>
 
<script>


export default {
    name: 'MsgBox',   
    props: {
        caption:{},
        show: {},
        msg:{},
        callback:{}
    },
    data() {
        return {
        }
    },

    methods: {
        init() {
          this.show = true;

        },
        close() {
          this.show = false;
          this.callback("close")
        },
        confirmClick() {
          this.show = false;
          this.callback("yes")
          //this.$emit('confirm');
        },
        cancelClick() {           
          this.show = false;
          this.callback("no")
        }

    }
}
</script>

<style>
    .dlg-msg-box {
        border-radius: 5px;
        width: 350px;
        height: 160px;
        background-color: #fff;
        padding: 30px;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
</style>

输入框组件代码

<template>
  <div v-if="show" class="mask">
    <div class="dlg-input-box flex flex-col">
      <div class="flex flex-space-between full-width">
        <div class="font-bold">{{caption}}</div>
        <div class="pointer" @click="close"><img class="logo-22" src="@/assets/images/guanbi.png"/></div>
      </div>
      <div class="margin-top-xl flex flex-col">
          <input class="input-box" placeholder="请输入" v-model="inputValue" ref="getfocus"/>
      </div>
      <div class="flex flex-end margin-top-xl">
        <div class="btn-huibai-auto pointer" style="width:80px" @click="cancelClick">取消</div>
        <div class="margin-left-m btn-blue-auto pointer" style="width:80px" @click="confirmClick">确定</div>
      </div>
    </div> 
  </div>
</template>
 
<script>


export default {
    name: 'InputBox',  
    props: {
        caption:{},
        value:{},
        show: {},
        callback: {}
    },
    watch: {
      show(val){
          if (val == true) {
              this.$nextTick(() => {
                  this.$refs.getfocus.focus();
              })
          }
      },
      value(val){
        this.inputValue = val;
      }

    },
    data() {
        return {
        }
    },
    
    methods: {
     
        close() {
          this.show = false;  
        },
        init() {
          this.show = true;

        },
        confirmClick() {
          if (this.inputValue == "") {
              this.$showToast({
                  msg: '内容未填写',
                  duration: 2000
              })               
              
          } else {
              this.callback(this.inputValue)
          }
          
        },
        cancelClick() {     
          this.show = false;      
        }

    }
}
</script>

<style>
    .dlg-input-box {
        border-radius: 5px;
        width: 350px;
        height: 160px;
        background-color: #fff;
        padding: 30px;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
</style>

提示框组件代码

<template>
  <div v-if="show" class="mask">
    <div class="dlg-show flex flex-col">
      <div class="flex flex-space-between full-width">
        <div class="font-bold">{{caption}}</div>
        <div class="pointer" @click="close"><img class="logo-22" src="@/assets/images/guanbi.png"/></div>
      </div>
      <div class="margin-top-l" style="height:45px;">{{msg}}</div>
      <div class="flex flex-end margin-top-xl">
        <div class="margin-left-m btn-blue-auto pointer full-width" @click="confirmClick">确定</div>
      </div>
    </div>
  </div>
</template>
 
<script>

export default {
    name: 'MsgShow',
    props: {
        caption:{},
        show: {},
        msg:{},
        callback:{}
    },
    data() {
        return {
        }
    },

    methods: {
      init() {
          this.show = true;
      },
      close() {
          this.show = false;
      },
      confirmClick() {
          this.show = false;
          this.callback("yes");

      }
    }
}
</script>

<style>
    .dlg-show {
        border-radius: 5px;
        width: 300px;
        height: 140px;
        background-color: #fff;
        padding: 30px;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
</style>

toast组件代码

<template>
    <div v-if="show" class="mask" style="z-index: 99999;">
      <div class="dlg-show1 flex flex-center-sp flex-center-cz">
        <div>{{msg}}</div>
      </div>
    </div>
  </template>
   
  <script>
  
  export default {
      name: 'Toast',   
      props: {
          show: {},
          msg:{},
          duration: {
            type: Number,
            default: 2000
        }
      },
      data() {
          return {
            timerDuration: null
          }
      },
      watch: {
        show(n) {
            if (!n && this.timerDuration) {
                clearTimeout(this.timerDuration)
                this.timerDuration = null
            }
        }

      },
      methods: {
        init() {
            this.show = true
            this.timerDuration = setTimeout(() => {
                this.show = false
            }, this.duration)
        },
        clear() {
            this.show = false
        } 
      }
  }
  </script>
  
  <style>
      .dlg-show1 {
          border-radius: 5px;
          width: 300px;
          background-color: #ffffff;
          padding: 10px;
          height: 30px;
          position: absolute;
          margin-left: auto;
          margin-right: auto;
          top: 20px;
          left: 0;
          right: 0;
          bottom: 0;
      }
  </style>
  

js代码

import MsgBox from '@/components/MsgBox.vue'
let ConfirmConstructor, instance

const showMsgBox = {
  install(Vue) {
    ConfirmConstructor = Vue.extend(MsgBox)
    instance = new ConfirmConstructor().$mount()
    document.body.appendChild(instance.$el)

    Vue.prototype.$showMsgBox = options => {
      Object.assign(instance, options)
      instance.init()
    }

  }
}

export default showMsgBox
import InputBox from '@/components/InputBox.vue'
let ConfirmConstructor, instance

const showInputBox = {
  install(Vue) {
    ConfirmConstructor = Vue.extend(InputBox)
    instance = new ConfirmConstructor().$mount()
    document.body.appendChild(instance.$el)

    Vue.prototype.$showInputBox = options => {
      Object.assign(instance, options)
      instance.init()
    }

  }
}

export default showInputBox
import MsgShow from '@/components/MsgShow.vue'
let ConfirmConstructor, instance

const showMsgShow = {
  install(Vue) {
    ConfirmConstructor = Vue.extend(MsgShow)
    instance = new ConfirmConstructor().$mount()
    document.body.appendChild(instance.$el)

    Vue.prototype.$showMsgShow = options => {
      Object.assign(instance, options)
      instance.init()
    }

  }
}

export default showMsgShow
import Toast from '@/components/Toast.vue'
let ConfirmConstructor, instance

const showToast = {
  install(Vue) {
    ConfirmConstructor = Vue.extend(Toast)
    instance = new ConfirmConstructor().$mount()
    document.body.appendChild(instance.$el)

    Vue.prototype.$showToast = options => {
      Object.assign(instance, options)
      instance.init()
    }
    Vue.prototype.$showToast.clear = () => {
      instance.clear()
    }
  }
}

export default showToast

调用代码

<template>
    <div class="body">
      <div class="table">
        <div class="filter font-bold">vue自定义全局弹出询问框、输入框、提示框、toast</div>
        <div class="padding-left-l padding-right-l">

          <div class="pointer cannotselect margin-top-l margin-left-l btn-blue-full" @click="MsgBoxClick">询问框</div>
          <div class="pointer cannotselect margin-top-l margin-left-l btn-blue-full" @click="InputBoxClick">输入框</div>
          <div class="pointer cannotselect margin-top-l margin-left-l btn-blue-full" @click="MsgShowClick">提示框</div>
          <div class="pointer cannotselect margin-top-l margin-left-l btn-blue-full" @click="ToastClick">Toast</div>
          
        </div>  

      </div>
    </div>
</template>

<script>
/*
       名称:vue自定义全局弹出询问框、输入框、提示框、toast
       功能:自定义属于自己的弹出窗,涉及到技术点:遮罩层样式,自定义组件,子组件套用组件,子组件调用父组件方法,组件属性监听,输入框默认获得焦点,输入框数据双向绑定        
       作者:唐赢   
       时间:2023-3-5
*/

  export default {
    name: 'Main',
    data () {
      return {        
      }
    },
    methods: {
      MsgBoxClick() {
        this.$showMsgBox({
          caption:"询问",
          msg: '确定要删除该条记录吗?',
          callback:(data) => {
            if (data == "yes") {
              this.$showToast({
                msg: '点了确定',
                duration: 2000
              })
            } else {
              this.$showToast({
                msg: '点了取消',
                duration: 2000
              })
            }
          }
        })

      },
      InputBoxClick() {
        this.$showInputBox({
          caption:"提示",
          inputValue: '标题1',
          callback:(data) => {
            console.log("最新的值", data);
          }
        })
      },     
      MsgShowClick() {
        this.$showMsgShow({
          caption:"提示",
          msg: '操作完毕!',
          callback:(data) => {
            console.log(data);
          }
        })        

      },
      ToastClick(){
        this.$showToast({
          msg: '网络错误',
          duration: 2000
        })

      }
    },

    
  }
</script>

  <!-- Add "scoped" attribute to limit CSS to this component only -->
  <style scoped>
  .body {
    display: flex;
    justify-content: center;
    margin-top: 73px;
    width: 100%;    
  }
  .table {
    background-color: #fff;
    width: 1080px;
    min-height: 800px;
    box-shadow: 0px 3px 6px rgba(0, 0, 0, .1);
    margin-bottom: 10px;
  }
  .filter {
    display: flex;
    height: 60px;
    align-items:center;
    background-color: #fff;
    font-size: 18px;
    justify-content: center;
    border-bottom: 1px solid rgba(0, 0, 0, .2);;
  }


  </style>
  

main.js代码

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import "./assets/css/base.css"
import ShowToast from './assets/js/toast'
import ShowInputBox from './assets/js/inputBox'
import ShowMsgBox from './assets/js/msgBox'
import ShowMsgShow from './assets/js/msgShow'

Vue.use(ShowToast)
Vue.use(ShowInputBox)
Vue.use(ShowMsgBox)
Vue.use(ShowMsgShow)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

操作演示地址:https://lusun.com/v/cR9piucUW1r

源码地址:https://download.csdn.net/download/gdgztt/87535048文章来源地址https://www.toymoban.com/news/detail-771658.html

到了这里,关于vue自定义全局弹出询问框、输入框、提示框、toast(附源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Vue中 全局限制输入特殊字符

    传送门:Vue实现自定义指令(directive)及应用场景 背景:开发中遇到的表单输入,常常会限制特殊字符的输入 以满足安全性测试的要求。 1. 单独处理每个文本框 这样每个输入框单独处理,工作量较大且不好维护,所以需要自定义一个指令来统一实现这一需求。 2. 自定义指

    2024年02月15日
    浏览(32)
  • 【提示工程】询问GPT返回Json结构数据

    众所周知,我们可以通过构建的Prompt获取期望的内容,但是通常都是以自然语言返回的,假如我们想得到结构化的数据,比如Json,XML那么怎么办,这篇文章给你一个思路。 要实现询问大模型后返回结构化的数据,首先能想到的是可以通过在提示末尾添加以 JSON 格式提供您的

    2024年02月09日
    浏览(24)
  • vue如何定义:全局变量、全局方法

    开发中会经常用到一些常用的变量和方法   例如ajax这种 1.定义专用模块来配置全局变量 定义一个专用模块来配置全局变量,然后通过export暴露出去,在需要的组件引入global.vue  引入及使用 2.通过全局变量挂载到Vue.prototype 同上,定义一个专用模块来配置全局变量,然后通过

    2023年04月20日
    浏览(25)
  • 询问ChatGPT的高质量答案艺术——提示工程指南

    书籍笔记《The Art of Asking ChatGPT for High-Quality Answers: A complete Guide to Prompt Engineering Techniques》 注意:由于chatGPT回复字数限制,凡是在案例中涉及到长文本的不会截图 提示工程是创建提示、询问或指令的过程,用以指导像ChatGPT这样的语言模型的输出。它允许用户控制模型的输出

    2024年02月03日
    浏览(70)
  • vscode vue2 + volar 全局代码提示

    这几天更新vscode vetur的后  项目一直转loading加载不出来了,索性就直接换volar了。 一、基础配置 但是volar的配置在vue3和vue2里是不太一样的   需要一些额外的配置。记录一下踩坑。 首先vscode安装扩展 Vue Language Features (Volar) 、 TypeScript Vue Plugin (Volar)  并且卸载或者禁用掉原

    2024年02月16日
    浏览(28)
  • uniapp全局挂载uViewUI的u-toast消息通知组件(微信小程序)

    在使用uView消息通知组件的时候,必须在每个页面中手动添加一个标签,打上ref然后才能调用,非常的麻烦,能不能像ElementUI那样把组件挂载到Vue原型上从而能全局调用呢,个人也是尝试了很多方法,但是由于小程序的app.vue不能写页面,所以无法达到ElementUI那种方便的效果,

    2024年02月15日
    浏览(29)
  • vue2 el-table行悬停时弹出提示信息el-popover

    实现方法,用到了cell-mouse-enter、cell-mouse-leave两个事件,然后在表格的首列字段中,加个el-popover组件,当然你也可以选择在其他字段的位置来显示提示框,看自己的需求了。 示例代码: 在enter方法中,还可以根据row行数据进行一些处理,比如根据状态status来判断是否弹出提示

    2024年01月18日
    浏览(57)
  • React Native实现Toast轻提示和loading

    使用react native的小伙伴都知道,官方并未提供轻提示组件,只提供了ToastAndroid API,顾名思义,只能再安卓环境下使用,对于ios就爱莫能助,故此,只能通过官方的核心组件,自行封装,实现Toast功能 创建文件 首先我们需要创建一个Toast组件,引入对应需要的依赖,icon等等 声

    2024年02月10日
    浏览(25)
  • 【Vue】全局变量的定义及使用

    首先 声明Vue 使用全局变量的方法有很多,以下只是个人觉得比较简洁的2种。其中两者的第一步操作相同,即: 方法1:在main.js中直接将全局变量挂载到Vue.prototype 用时不用任何多余操作,直接调用 this.GLOBAL.name 即可。 方法2:在需要使用全局变量的页面引入global再使用

    2024年02月12日
    浏览(43)
  • vue3 ts 定义全局变量

    在 Vue3 中使用 TypeScript 定义全局变量可以这样做: 创建一个文件,如 global.d.ts ,并在其中声明全局变量。 在 main.ts 或其他入口文件中引入该文件。 在需要使用全局变量的地方直接使用即可。 注意,这种方式只能用于定义全局变量,不能用于定义全局函数或类。

    2024年02月17日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包