main.vue
<template>
<div v-show="show" class="message">
<h2>title</h2>
<div>
<button @click="handleConfirm">确认<button>
<button @click="handleConfirm">取消</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
show: false,
}
},
methods: {
handleConfirm() {
this.show = false
}
},
}
</script>
<style>
.message {
width: 300px;
height: 300px;
}
</style>
main.js
import Vue from 'vue';
import Main from './main.vue';
const MainConstructor = Vue.extend(Main)
let instance // 单例模式
const Message = () => {
if(!instance) {
instance = new MainConstructor().$mount()
// instance.show = true
document.body.appendChild(instance.$el)
} else {
}
instance.show = true
}
export default Message // 第一次调用Message()会把弹窗挂载到body上,后面再调用的时候直接让弹窗显示,可以传递参数,instance.show或instance.$data.show访问data数据
参考:
element: https://github.com/ElemeFE/element/blob/dev/packages/message/src/main.js
$mounted(): https://cn.vuejs.org/v2/api/#vm-mount
本文介绍了一个基于Vue的弹窗组件实现,通过单例模式确保弹窗实例的唯一性,并利用Vue的生命周期钩子和DOM操作实现了弹窗的动态显示与隐藏。组件支持参数传递,便于复用。
6480

被折叠的 条评论
为什么被折叠?



