效果图:
下面是源码:
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
{{title}}
通过添加实例方法,把插件添加到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实现模态提示窗内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!