Vue手写一个弹出框复用组件

虽然现在UI框架很多有弹出框这个组件,但是自己写是不是更能体现自己的能力呢,样式也可以随意改变,哈哈,个人觉得是这样

首先创建一个message文件,里面创建一个messageBox.vue

<template>
    <div class="messageBox">
        <h2>{{ title }}</h2>
        <p>{{ content }}</p>
        <div>
            <div v-if="cancel" @touchstart="handleCancel">{{ cancel }}</div>
            <div v-if="ok" @touchstart="handleOk">{{ ok }}</div>
        </div>
    </div>
</template>

<script>

export default {
}
</script>
<style lang='scss' scoped>
.messageBox {
    width: 200px;
    height: 120px;
    border: 1px #ccc solid;
    border-radius: 4px;
    background-color: red;
    position: absolute;
    left: 50%;
    color: #fff;
    top: 50%;
    transform: translate(-50%,-50%);
    h2 {
        text-align: center;
        font-size: 18px;
    }
    p {
        text-align: center;
    }
    > div {
        display: flex;
        position: absolute;
        bottom: 0;
        width: 100%;
        border-top: 1px solid #ccc;
        div {
            flex: 1;
            text-align: center;
            line-height: 30px;
            border-right: 1px #ccc solid;
        }
        div:last-child {
            border: none;
        }
    }
}
</style>

同级创建一个index.js文件

import Vue from ‘vue’
import MessageBox from ‘./messageBox’

export var messageBox = (function () {

return function (opts) { //配置参数
    var defaults = { //默认值
        title: '',
        content: '',
        cancel: '',
        ok: '',
        handleCancel: null,
        handleOk: null
    }
    var MyComponent = Vue.extend(MessageBox)

    for (var attr in opts) {
        defaults[attr] = opts[attr]
    }
    var vm = new MyComponent({
        el: document.createElement('div'),
        data: {
            title: defaults.title,
            content: defaults.content,
            cancel: defaults.cancel,
            ok: defaults.ok
        },
        methods: {
            handleCancel() {
                defaults.handleCancel && defaults.handleCancel.call(this)
                document.body.removeChild(vm.$el)
            },
            handleOk() {
                defaults.handleOk && defaults.handleOk.call(this)
                document.body.removeChild(vm.$el)
            }
        }
    })
    document.body.appendChild(vm.$el)
}
 })();

下面演示一下怎么用这个组件

 <button @click="open()" style="background:red">出来</button>
 
 import {messageBox} from './message'
 methods:{
   open(){
     messageBox({
    title:'定位',
    content:'武汉',
    cancel:'取消',
    ok:'切换定位',
    handleCancel(){    //取消事件
      console.log('我取消了')
    },
    handleOk(){    //切换事件
      console.log('切换位置了')
    }
 })
  },
 }

效果如下

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 是一款流行的 JavaScript 架,用于构建用户界面和单页面应用程序。手写原生弹出可以通过 Vue 3 的组件来实现,以下是一个简单的示例: 1. 新建一个名为 "MyAlert" 的 Vue 组件,该组件包含一个 "message" 属性和一个方法 "closeAlert"。 ``` <template> <div class="my-alert" v-show="visible"> <div class="my-alert-mask" @click="closeAlert"></div> <div class="my-alert-content"> <p>{{ message }}</p> <button @click="closeAlert">确定</button> </div> </div> </template> <script> export default { props: { message: { type: String, default: "", }, }, data() { return { visible: true, }; }, methods: { closeAlert() { this.visible = false; }, }, }; </script> <style scoped> .my-alert { position: fixed; top: 0; left: 0; right: 0; bottom: 0; } .my-alert-mask { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); } .my-alert-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; } </style> ``` 2. 在需要弹出的地方使用该组件,并传递要显示的消息。 ``` <template> <div> <button @click="showAlert">弹出</button> <my-alert :message="alertMsg" v-if="isShowAlert" @closeAlert="isShowAlert=false"></my-alert> </div> </template> <script> import MyAlert from "./MyAlert.vue"; export default { components: { MyAlert, }, data() { return { isShowAlert: false, alertMsg: "", }; }, methods: { showAlert() { this.isShowAlert = true; this.alertMsg = "这是一个弹出"; }, }, }; </script> ``` 这样就可以实现一个简单的手写原生弹出。相比于使用浏览器的原生弹出,自定义的弹出更加灵活,并且可以根据需求进行自由扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值