博客
关于我
封装vue的弹窗组件
阅读量:328 次
发布时间:2019-03-04

本文共 1462 字,大约阅读时间需要 4 分钟。

工具函数创建组件实例

工具函数说明

为了实现组件实例的创建与管理,我们编写了一个工具函数 create,该函数负责根据传入的组件类和属性参数,创建一个新的 Vue 实例,并将其挂载至页面中。

工具函数实现步骤

  • 创建 Vue 实例:使用 Vue 框架创建一个新的实例,并配置其渲染逻辑
  • 渲染组件:通过 render 函数,将传入的组件进行渲染
  • 挂载实例:将创建好的 Vue 实例挂载到 HTML 页面的 body 标签上
  • 获取组件实例:通过 Vue 实例获取创建的组件实例
  • 生命周期管理:定义组件的生命周期回调函数,确保组件的创建和销毁能够被正确管理
  • // 创建指定组件实例并挂载于body上import Vue from 'vue';export default function create(Component, props) {    const vm = new Vue({        render(h) {            return h(Component, {                props            });        }    }).$mount();    const comp = vm.$children[0];    document.body.appendChild(vm.$el);    // 定义清理函数    comp.remove = () => {        document.body.removeChild(vm.$el);        vm.$destroy();    };    return comp;}

    Notice 组件

    组件代码

    组件说明

  • 模板部分:使用 v-if 指令控制组件的显示与隐藏
  • 属性定义:组件接受三个属性 titlemessageduration
  • 生命周期方法
    • show():用于显示组件,并设置自动关闭的时间
    • hide():用于隐藏组件并移除实例
  • 双向数据绑定:通过 data()methods 属性管理组件的状态和交互逻辑
  • 组件使用场景

    示例应用

    在登录功能中,通常需要在用户输入验证通过后,显示一个通知窗口。可以通过以下方式实现:

    methods: {    onLogin() {        return this.$refs.loginForm.validate((isValid) => {            if (!isValid) return;            const notice = create(Notice, {                title: "欢迎登录",                message: "请根据提示进行登录",                duration: 3000            });            notice.show();        });    }}

    使用说明

  • 组件引用:在需要显示通知的位置,引用 Notice 组件
  • 属性传递:通过 props 属性将组件的属性进行传递
  • 生命周期管理:通过 create 工具函数确保组件的生命周期被正确管理
  • 自动关闭:通过 duration 属性设置自动关闭时间,确保页面不会被长时间占用
  • 转载地址:http://tvsh.baihongyu.com/

    你可能感兴趣的文章
    OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
    查看>>
    OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
    查看>>
    OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
    查看>>
    OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
    查看>>
    OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
    查看>>
    OAuth2.0_授权服务配置_三项内容_Spring Security OAuth2.0认证授权---springcloud工作笔记141
    查看>>
    OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
    查看>>
    OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
    查看>>
    OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
    查看>>
    OAuth2.0_授权服务配置_授权码模式_Spring Security OAuth2.0认证授权---springcloud工作笔记144
    查看>>
    OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
    查看>>
    OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
    查看>>
    OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
    查看>>
    oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
    查看>>
    OAuth2.0四种模式的详解
    查看>>
    OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
    查看>>
    oauth2登录认证之SpringSecurity源码分析
    查看>>
    OAuth2:项目演示-模拟微信授权登录京东
    查看>>
    OA系统多少钱?OA办公系统中的价格选型
    查看>>
    OA系统选型:选择好的工作流引擎
    查看>>