通常情况下,我们封装组件,然后调用,通过import 引入,再书写components,再调用,遇到使用频率很高的弹框,就是显得很麻烦,冗余。
案例以 uniapp的uview插件的u-model为例
1.首先创建 index.vue, index.js文件 2.index.vue 页面
<template>
<view class="components-message">
<u-modal
v-model="showModel"
:content="content"
:title="title"
:show-cancel-button='showCancel'
@cancel="handleClose"
@confirm="handleBtn"
:confirm-text='confirmText'
:cancel-text='cancelText'
:mask-close-able='maskcloseAble'
></u-modal>
</view>
</template>
<script>
export default {
name: "Cmessage",
components: {},
data() {
return {
showModel: false,
title:'提示',
content: "",
showCancel:true,
confirmText:'确认',
cancelText:'取消',
maskcloseAble:true,
};
},
computed: {},
watch: {},
onLoad(options) {},
onShow() {},
onReady() {},
methods: {
emitBtn() {},
emitClose() {},
handleClose() {
this.$el.parentNode.removeChild(this.$el);
this.emitClose();
},
handleBtn() {
this.$el.parentNode.removeChild(this.$el);
this.emitBtn();
},
},
onHide() {},
onUnload() {},
};
</script>
<style lang='scss'>
</style>
3 .index.js
import Vue from 'vue';
import Message from './index.vue';
const MessageConstructor = Vue.extend(Message);
export const Cmessage = (options,methods = {}) => {
const instance = new MessageConstructor(
{
data: options,
methods
}
).$mount();
document.body.appendChild(instance.$el);
}
4.使用 main.js 全局注册
import { Cmessage } from '@/components/message/index.js';
Vue.prototype.$Cmessage = Cmessage
5.使用
this.$Cmessage({showModel:true,content: '登录状态已过期,您可以继续留在该页面,或者重新登录',maskcloseAble:false}, {
emitBtn () {
store.dispatch('LogOut').then((res) => {
uni.reLaunch({
url:'/pages/login/index'
})
})
},
emitClose() {
console.log('用户点击取消');
},
});
6.效果图 帮助到你的话点赞支持一下~
|