Vue3 优雅的模态框封装方案 - 初探

本文探讨Vue3中优雅地封装模态框的方法,涉及虚拟节点创建、渲染及Vue3的瞬移组件(Teleport)。通过封装模态框,实现模板和JS代码中的灵活调用,同时保持组件库模态框的复用性和扩展性。文章详细讲解了如何使用h函数创建虚拟节点,render函数渲染至DOM以及Teleport组件的使用特性。
摘要由CSDN通过智能技术生成

my-blog:https://tuimao233.gitee.io/mao-blog/

Vue3 优雅的模态框封装方法 - 初探

Vue3 优雅的模态框封装方法 - 实践

想必大家使用 Vue 开发时,都有使用过 Element 或 Ant Design of Vue 的模态框

例如 Ant Design of Vue 中的 a-message:

  <a-modal v-model="visible" title="Title" on-ok="handleOk">
    自定义内容
  </a-modal>

又或者直接在 js 代码中直接调起的 ElMessageBox:

import {
    ElMessageBox } from 'element-plus';

ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
    .then(()=> {
   
    })
    .
为了封装vue-ueditor-wrap,可以按照以下步骤进行操作: 1.安装vue-ueditor-wrap和ueditor ```shell npm install vue-ueditor-wrap ueditor --save ``` 2.在main.js中引入ueditor ```javascript import Vue from 'vue' import App from './App.vue' import UEditor from 'vue-ueditor-wrap' import 'ueditor/ueditor.config.js' import 'ueditor/ueditor.all.min.js' import 'ueditor/lang/zh-cn/zh-cn.js' Vue.component('u-editor', UEditor) new Vue({ el: '#app', render: h => h(App) }) ``` 3.在组件中使用vue-ueditor-wrap ```vue <template> <div> <u-editor v-model="content"></u-editor> </div> </template> <script> export default { data() { return { content: '' } } } </script> ``` 4.封装vue-ueditor-wrap为一个独立的组件 ```vue <template> <div> <u-editor v-model="content"></u-editor> </div> </template> <script> import UEditor from 'vue-ueditor-wrap' import 'ueditor/ueditor.config.js' import 'ueditor/ueditor.all.min.js' import 'ueditor/lang/zh-cn/zh-cn.js' export default { name: 'VueUeditorWrap', components: { UEditor }, props: { value: { type: String, default: '' } }, data() { return { content: this.value } }, watch: { value(val) { this.content = val }, content(val) { this.$emit('input', val) } } } </script> ``` 5.在其他组件中使用封装好的vue-ueditor-wrap组件 ```vue <template> <div> <vue-ueditor-wrap v-model="content"></vue-ueditor-wrap> </div> </template> <script> import VueUeditorWrap from './VueUeditorWrap.vue' export default { components: { VueUeditorWrap }, data() { return { content: '' } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值