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

时间:2022-08-24 21:55:04

效果图:

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

下面是源码:

index.js

  1. import Vue from 'vue';
  2. import model from './model.vue';
  3.  
  4. export default {
  5. install(Vue) {
  6.  
  7. const defaults = {
  8. show: false,
  9. mask: true,
  10. title: '提示',
  11. content: '这是正文',
  12. confirmButton: true,
  13. cancelButton: true,
  14. confirmText: '确认',
  15. cancelText: '取消',
  16. cancelCallBack: () => {},
  17. confirmCallBack: () => {}
  18. };
  19.  
  20. const modelVueConstructor = Vue.extend(model);
  21.  
  22. Vue.prototype.$model = (options = {}) => {
  23. if (Vue.prototype.$isServer) return;
  24. options = Object.assign({}, defaults, options);
  25. let parent = document.body ;
  26. let instance = new modelVueConstructor({
  27. el: document.createElement('div'),
  28. data: options
  29. });
  30. parent.appendChild(instance.$el);
  31.  
  32. return instance;
  33. };
  34.  
  35. },
  36. };

model.vue

  1. <template>
  2. <div v-if="show" class="model-container">
  3. <div class="model-main">
  4. <div class="model-title">{{title}}</div>
  5. <div class="model-content" v-html="content"></div>
  6. <div class="model-buttons">
  7. <button v-if="cancelButton" @click="cancelClick" class="button">{{cancelText}}</button>
  8. <button v-if="confirmButton" @click="confirmClick" class="button confirm">{{confirmText}}</button>
  9. </div>
  10. </div>
  11. <div v-show="mask" class="model-mask"></div>
  12. </div>
  13.  
  14. </template>
  15.  
  16. <script type="text/babel">
  17. export default {
  18. data() {
  19. return {
  20. show: false,
  21. mask: true,
  22. title: '提示',
  23. content: '这是正文',
  24. confirmButton: true,
  25. cancelButton: true,
  26. confirmText: '确认',
  27. cancelText: '取消',
  28. cancelCallBack: () => {},
  29. confirmCallBack: () => {}
  30. };
  31. },
  32. methods: {
  33. cancelClick(){
  34. this.show = false;
  35. this.cancelCallBack();
  36. },
  37. confirmClick(){
  38. this.show = false;
  39. this.confirmCallBack();
  40. }
  41. }
  42. };
  43. </script>
  44. <style lang="less" scoped>
  45. .model-container{
  46. width: 100%;
  47. height: 100vh;
  48. position: fixed;
  49. top: 0;
  50. left: 0;
  51. z-index: var(--model-index);
  52. display: flex;
  53. justify-content: center;
  54. align-items: center;
  55. .model-main{
  56. position: relative;
  57. z-index: 9;
  58. width: 80%;
  59. background-color: #ffffff;
  60. border-radius: 10px;
  61. overflow: hidden;
  62. text-align: center;
  63. .model-title{
  64. font-size: 18px;
  65. color: #333;
  66. width: 100%;
  67. padding: 18px;
  68. font-weight: bold;
  69. overflow: hidden;
  70. white-space: nowrap;
  71. text-overflow: ellipsis;
  72. }
  73. .model-content{
  74. font-size: 16px;
  75. color: #666;
  76. padding: 10px;
  77. padding-top: 0px;
  78. padding-bottom: 20px;
  79. }
  80. .model-buttons{
  81. width: 100%;
  82. display: flex;
  83. align-items: center;
  84. .button{
  85. flex: 1;
  86. padding: 18px 10px;
  87. overflow: hidden;
  88. white-space: nowrap;
  89. text-overflow: ellipsis;
  90. font-size: 16px;
  91. outline: none;
  92. background-color: #ffffff;
  93. border-top: 1px solid #f2f2f2;
  94. border-right: 1px solid #f2f2f2;
  95. &.confirm{
  96. color: var(--theme);
  97. font-weight: bold;
  98. }
  99. &:last-child{
  100. border-right: 0;
  101. }
  102. &:active{
  103. background-color: #f2f2f2;
  104. }
  105. }
  106. }
  107. }
  108. .model-mask{
  109. width: 100%;
  110. height: 100%;
  111. position: absolute;
  112. top: 0;
  113. left: 0;
  114. z-index: 1;
  115. background-color: rgba(0,0,0,0.45);
  116. }
  117. }
  118. </style>

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

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

main.js

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

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

  1. this.$model({
  2. show: true,
  3. title: "提示",
  4. content: "提示内容",
  5. cancelButton: true,
  6. confirmCallBack: () => {
  7. console.log("确认");
  8. },
  9. cancelCallBack: () => {
  10. console.log("取消");
  11. }
  12. });

总结

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

原文链接:https://blog.csdn.net/qq_41756580/article/details/108096793