vue3自定义插件(如何将弹窗组件挂载全局)使用

#实现功能:自定义一个提示语弹窗,挂载在vue实例上全局使用

#效果图如下:

实现步骤:

1.先创建一个confirm.vue文件,这是我们的弹窗组件

<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    message: string; // 提示信息
    title: string; // 标题
    show: boolean; // 是否显示
    onConfirm: Function; // 确认按钮回调
    onCancel: Function; // 取消按钮回调
  }>(),
  {
    title: '提示', //定义默认值
    show: false,
  },
);
const emit = defineEmits(['update:modelValue', 'onConfirm', 'onCancel']);

const show_ = computed({
  get: () => props.show,
  set: (val) => {
    emit('update:modelValue', val);
  },
});

const onConfirm = () => {
  props.onConfirm();
};
const onCancel = () => {
  props.onCancel.();
  show_.value = false;
};

</script>
<template>
  <!-- 这是自己封装的一个弹窗组件 ,就放放上来了,下面注释的el-dialog一样可以实现 -->
  <ft-dialog
    :title="title"
    v-model="show_"
    @on-close="onCancel"
    @on-confirm="onConfirm"
  >
    <p>{{ message }}</p>
  </ft-dialog>

  <!-- <el-dialog v-model="show_" :title="title" width="500" @close="onCancel">
    <div>{{ message }}</div>
    <template #footer>
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </template>
  </el-dialog> -->
</template>

<style scoped lang="scss"></style>

2.创建一个confirmInstall.ts 

import type { App, } from 'vue'
import { render, createVNode, } from 'vue'
import confirm from '@/views/demo/confirm.vue'

interface DialogOptions {
    title?: string;
    message: string;
    onConfirm?: Function;
    onCancel?: Function;
}

export default {
    install: (app: App) => {
        // 添加全局方法 $ftConfirm
        app.config.globalProperties.$ftConfirm = (options: DialogOptions) => {
            const show = ref(true)
            const wrapper = document.createElement('div')
            wrapper.id = 'ft-confirm'; // 创建一个id=ft-confirm的容器
            let vnode = createVNode(confirm); // 创建一个vnode
            const close = () => {
                // 关闭弹窗移除标签
                document.body.removeChild(document.getElementById('ft-confirm') as HTMLDivElement);
            }
            const dialogProps = reactive({
                onConfirm: options.onConfirm ? () => { options.onConfirm?.(); close() } : close,
                onCancel: close,
                title: options.title,
                message: options.message,
                show: show.value,
            });
            vnode = createVNode(confirm, dialogProps);
            render(vnode, wrapper); // 渲染组件
            document.body.appendChild(wrapper); // 添加到body
        };
    }
}

3.main.ts

//$ftConfirm声明 放置报错 和 智能提示
declare module '@vue/runtime-core' {
    export interface ComponentCustomProperties {
        $ftConfirm: any;
    }
}

import confirm from '@/views/demo/confirmInstall';
const app = createApp(App);
app.use(confirm);
app.mount('#app');

4. 使用方法

<script setup lang="ts">
import { getCurrentInstance } from 'vue';
const instance = getCurrentInstance()?.appContext.config.globalProperties; // 获取全局属性

const open = () => {
  instance?.$ftConfirm({
    title: '这是我的标题',
    message: '哈哈哈哈哈哈哈哈',
    onConfirm: () => {
      console.log('confirm---open11');
    },
  });
};
const open2 = () => {
  instance?.$ftConfirm({
    message: '笑笑笑笑笑了一下',
    onConfirm: () => {
      console.log('confirm---open2');
    },
  });
};
</script>
<template>
  <div>demo</div>
  <button @click="open">弹出1</button>
  <br />
  <button @click="open2">弹出2</button>
  <br />
</template>

<style scoped lang="scss"></style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3封装全局弹窗组件的步骤如下: 1. 创建一个Vue实例,作为全局弹窗组件的容器。可以使用`createApp`方法创建Vue实例,并将其挂载到一个DOM元素上。 2. 在全局弹窗组件上定义必要的属性和方法。比如,可以定义一个`visible`属性控制弹窗的显示与隐藏,一个`title`属性用于显示弹窗的标题,一个`content`属性用于显示弹窗的内容等。 3. 在全局弹窗组件内部实现弹窗的样式和交互逻辑。可以使用Vue的模板语法和样式定义实现弹窗的外观和样式效果,并通过Vue的响应式特性,实现弹窗的交互逻辑,比如点击关闭按钮时隐藏弹窗。 4. 添加全局方法,在Vue实例的原型上添加一个方法,用于在任意组件中调用弹窗组件。可以使用`app.config.globalProperties`来添加全局方法,以便在任何地方都可以访问到该方法。 5. 在组件使用全局弹窗组件。在需要显示弹窗组件中,通过调用全局方法来调用弹窗组件。可以通过传递参数的方式,动态设置弹窗的内容和样式。 6. 在全局弹窗组件的内部实现弹窗的生命周期钩子函数,比如`mounted`函数用于在弹窗组件挂载到DOM后执行相应的逻辑。 通过以上步骤,就可以封装一个可在任何组件使用全局弹窗组件。在使用过程中,只需要调用全局方法,传入相应的参数,即可显示自定义弹窗内容和样式,提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值