vue插件--仿微信小程序showModel实现模态提示窗功能

 更新时间:2020年08月19日 14:29:52   作者:专业前端小白  
这篇文章主要介绍了vue插件--仿微信小程序showModel实现模态提示窗,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

效果图:

下面是源码:

index.js

import Vue from 'vue';
import model from './model.vue';
 
 
export default {
 install(Vue) {
 
 const defaults = {
  show: false,
  mask: true,
  title: '提示',
  content: '这是正文',
  confirmButton: true,
  cancelButton: true,
  confirmText: '确认',
  cancelText: '取消',
  cancelCallBack: () => {},
  confirmCallBack: () => {}
 };
 
 const modelVueConstructor = Vue.extend(model);
 
 Vue.prototype.$model = (options = {}) => {
  if (Vue.prototype.$isServer) return;
  options = Object.assign({}, defaults, options);
  let parent = document.body ;
  let instance = new modelVueConstructor({
  el: document.createElement('div'),
  data: options
  });
  parent.appendChild(instance.$el);
 
  return instance;
 };
 
 },
};

model.vue

<template>
 <div v-if="show" class="model-container">
 <div class="model-main">
  <div class="model-title">{{title}}</div>
  <div class="model-content" v-html="content"></div>
  <div class="model-buttons">
  <button v-if="cancelButton" @click="cancelClick" class="button">{{cancelText}}</button>
  <button v-if="confirmButton" @click="confirmClick" class="button confirm">{{confirmText}}</button>
  </div>
 </div>
 <div v-show="mask" class="model-mask"></div>
 </div>
 
</template>
 
<script type="text/babel">
 export default {
 data() {
 return {
 show: false,
 mask: true,
 title: '提示',
 content: '这是正文',
 confirmButton: true,
 cancelButton: true,
 confirmText: '确认',
 cancelText: '取消',
 cancelCallBack: () => {},
 confirmCallBack: () => {}
 };
 },
 methods: {
 cancelClick(){
  this.show = false;
  this.cancelCallBack();
 },
 confirmClick(){
  this.show = false;
  this.confirmCallBack();
 }
 }
 };
</script>
<style lang="less" scoped>
 .model-container{
 width: 100%;
 height: 100vh;
 position: fixed;
 top: 0;
 left: 0;
 z-index: var(--model-index);
 display: flex;
 justify-content: center;
 align-items: center;
 .model-main{
  position: relative;
  z-index: 9;
  width: 80%;
  background-color: #ffffff;
  border-radius: 10px;
  overflow: hidden;
  text-align: center;
  .model-title{
  font-size: 18px;
  color: #333;
  width: 100%;
  padding: 18px;
  font-weight: bold;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  }
  .model-content{
  font-size: 16px;
  color: #666;
  padding: 10px;
  padding-top: 0px;
  padding-bottom: 20px;
  }
  .model-buttons{
  width: 100%;
  display: flex;
  align-items: center;
  .button{
   flex: 1;
   padding: 18px 10px;
   overflow: hidden;
   white-space: nowrap;
   text-overflow: ellipsis;
   font-size: 16px;
   outline: none;
   background-color: #ffffff;
   border-top: 1px solid #f2f2f2;
   border-right: 1px solid #f2f2f2;
   &.confirm{
   color: var(--theme);
   font-weight: bold;
   }
   &:last-child{
   border-right: 0;
   }
   &:active{
   background-color: #f2f2f2;
   }
  }
  }
 }
 .model-mask{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  background-color: rgba(0,0,0,0.45);
 }
 }
</style>

通过添加实例方法,把插件添加到vue.prototype上来实现。

在使用之前需要将插件挂载到Vue全局实例上:

main.js

import VueModel from './components/model/index.js';
Vue.use(VueModel);

完成上述条件后,就可以在你的vue项目中使用啦:

this.$model({
 show: true,
 title: "提示",
 content: "提示内容",
 cancelButton: true,
 confirmCallBack: () => {
 console.log("确认");
 },
 cancelCallBack: () => {
 console.log("取消");
 }
});

总结

到此这篇关于vue插件--仿微信小程序showModel实现模态提示窗的文章就介绍到这了,更多相关微信小程序showModel实现模态提示窗内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Antd的Table组件嵌套Table以及选择框联动操作

    Antd的Table组件嵌套Table以及选择框联动操作

    这篇文章主要介绍了Antd的Table组件嵌套Table以及选择框联动操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • vuex中Getter的用法详解

    vuex中Getter的用法详解

    这篇文章主要给大家介绍了关于Vuex中Getter的基本使用教程,getter相当于Vuex中的计算属性 对 state 做处理再返回,本文通过示例代码将Getter介绍的非常详细,需要的朋友可以参考下
    2021-07-07
  • Vue中的MVVM模式使用及说明

    Vue中的MVVM模式使用及说明

    这篇文章主要介绍了Vue中的MVVM模式使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • vue小白入门教程

    vue小白入门教程

    vue是一套用于构建用户界面的渐进式框架,本文通过实例给大家介绍了vue入门教程适用小白初学者,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2018-04-04
  • iview实现动态表单和自定义验证时间段重叠

    iview实现动态表单和自定义验证时间段重叠

    这篇文章主要介绍了iview实现动态表单和自定义验证时间段重叠,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • vue cli3.x打包后如何修改生成的静态资源的目录和路径

    vue cli3.x打包后如何修改生成的静态资源的目录和路径

    这篇文章主要介绍了vue cli3.x打包后如何修改生成的静态资源的目录和路径,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • vue3原始值响应方案及响应丢失问题解读

    vue3原始值响应方案及响应丢失问题解读

    这篇文章主要介绍了vue3原始值响应方案及响应丢失问题解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • VueAwesomeSwiper在VUE中的使用以及遇到的一些问题

    VueAwesomeSwiper在VUE中的使用以及遇到的一些问题

    这篇文章主要介绍了VueAwesomeSwiper在VUE中的使用以及遇到的一些问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • vue-element-admin配置小结

    vue-element-admin配置小结

    本文主要介绍了vue-element-admin配置小结,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • vue3中不支持.sync语法糖的解决方法

    vue3中不支持.sync语法糖的解决方法

    在 Vue 3 中,.sync 修饰符已经被移除,在 Vue 2 中,.sync 修饰符是一个语法糖,用于简化子组件和父组件之间的双向数据绑定,那么本文将给大家介绍一下vue3中不支持.sync语法糖的解决方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01

最新评论