vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue弹出是否继续操作

vue弹出是否继续操作的方法实例

作者:悟能不能悟

在Vue中弹出确认框,通常我们会借助UI组件库来实现,因为这样可以很方便地集成到Vue项目中,并且这些库已经为我们处理好了样式和交互,这篇文章主要介绍了vue弹出是否继续操作的相关资料,需要的朋友可以参考下

Vue确认操作弹窗设计

我将设计一个优雅的Vue确认操作弹窗,让用户在执行重要操作前进行确认。

设计思路

实现方案

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue确认操作弹窗</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            padding: 20px;
            color: #333;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 30px;
        }
        
        header {
            text-align: center;
            margin-bottom: 40px;
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.5rem;
        }
        
        .subtitle {
            color: #7f8c8d;
            font-size: 1.2rem;
        }
        
        .demo-section {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }
        
        h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        
        .button-group {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin: 20px 0;
        }
        
        button {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s ease;
            font-size: 16px;
        }
        
        .btn-primary {
            background-color: #3498db;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
        }
        
        .btn-danger {
            background-color: #e74c3c;
            color: white;
        }
        
        .btn-danger:hover {
            background-color: #c0392b;
            transform: translateY(-2px);
        }
        
        .btn-warning {
            background-color: #f39c12;
            color: white;
        }
        
        .btn-warning:hover {
            background-color: #d35400;
            transform: translateY(-2px);
        }
        
        .btn-success {
            background-color: #2ecc71;
            color: white;
        }
        
        .btn-success:hover {
            background-color: #27ae60;
            transform: translateY(-2px);
        }
        
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 6px;
            border-left: 4px solid #3498db;
        }
        
        /* 弹窗样式 */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            animation: fadeIn 0.3s forwards;
        }
        
        .modal-content {
            background: white;
            border-radius: 10px;
            width: 90%;
            max-width: 500px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            transform: translateY(-20px);
            animation: slideUp 0.3s forwards;
            overflow: hidden;
        }
        
        .modal-header {
            padding: 20px 25px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-title {
            font-size: 1.4rem;
            font-weight: 600;
            color: #2c3e50;
        }
        
        .modal-close {
            background: none;
            border: none;
            font-size: 1.5rem;
            color: #7f8c8d;
            cursor: pointer;
            padding: 5px;
        }
        
        .modal-body {
            padding: 25px;
        }
        
        .modal-message {
            font-size: 1.1rem;
            color: #555;
            line-height: 1.6;
        }
        
        .modal-footer {
            padding: 20px 25px;
            background-color: #f8f9fa;
            display: flex;
            justify-content: flex-end;
            gap: 15px;
        }
        
        .btn-cancel {
            background-color: #95a5a6;
            color: white;
        }
        
        .btn-cancel:hover {
            background-color: #7f8c8d;
        }
        
        .btn-confirm {
            background-color: #e74c3c;
            color: white;
        }
        
        .btn-confirm:hover {
            background-color: #c0392b;
        }
        
        /* 动画 */
        @keyframes fadeIn {
            to {
                opacity: 1;
            }
        }
        
        @keyframes slideUp {
            to {
                transform: translateY(0);
            }
        }
        
        .fade-out {
            animation: fadeOut 0.2s forwards;
        }
        
        @keyframes fadeOut {
            to {
                opacity: 0;
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            .demo-section {
                padding: 20px;
            }
            
            .button-group {
                flex-direction: column;
            }
            
            button {
                width: 100%;
            }
        }
        
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            padding: 20px;
            border-radius: 6px;
            overflow-x: auto;
            margin: 20px 0;
            font-family: 'Courier New', monospace;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="container">
            <header>
                <h1>Vue确认操作弹窗</h1>
                <p class="subtitle">优雅的用户确认交互设计</p>
            </header>
            
            <section class="demo-section">
                <h2>演示示例</h2>
                <p>点击下面的按钮体验不同类型的确认弹窗:</p>
                
                <div class="button-group">
                    <button class="btn-primary" @click="showDefaultConfirm">默认确认弹窗</button>
                    <button class="btn-danger" @click="showDeleteConfirm">删除确认</button>
                    <button class="btn-warning" @click="showWarningConfirm">警告确认</button>
                    <button class="btn-success" @click="showCustomConfirm">自定义弹窗</button>
                </div>
                
                <div class="result" v-if="lastAction">
                    <strong>操作结果:</strong> {{ lastAction }}
                </div>
            </section>
            
            <section class="demo-section">
                <h2>实现代码</h2>
                <p>以下是如何在Vue中实现确认弹窗的核心代码:</p>
                
                <div class="code-block">
                    &lt;!-- 确认弹窗组件 --&gt;
                    &lt;div class="modal-overlay" v-if="showModal" @click.self="closeModal"&gt;
                        &lt;div class="modal-content"&gt;
                            &lt;div class="modal-header"&gt;
                                &lt;h3 class="modal-title"&gt;{{ modalTitle }}&lt;/h3&gt;
                                &lt;button class="modal-close" @click="closeModal"&gt;&times;&lt;/button&gt;
                            &lt;/div&gt;
                            &lt;div class="modal-body"&gt;
                                &lt;p class="modal-message"&gt;{{ modalMessage }}&lt;/p&gt;
                            &lt;/div&gt;
                            &lt;div class="modal-footer"&gt;
                                &lt;button class="btn-cancel" @click="cancel"&gt;{{ cancelText }}&lt;/button&gt;
                                &lt;button class="btn-confirm" @click="confirm"&gt;{{ confirmText }}&lt;/button&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                </div>
                
                <div class="code-block">
                    // 使用方法
                    this.$confirm({
                        title: '确认操作',
                        message: '您确定要执行此操作吗?',
                        confirmText: '确定',
                        cancelText: '取消'
                    }).then(() => {
                        // 用户点击确定
                        console.log('操作已确认');
                    }).catch(() => {
                        // 用户点击取消
                        console.log('操作已取消');
                    });
                </div>
            </section>
        </div>
        
        <!-- 确认弹窗组件 -->
        <div class="modal-overlay" v-if="showModal" @click.self="closeModal">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="modal-title">{{ modalTitle }}</h3>
                    <button class="modal-close" @click="closeModal">&times;</button>
                </div>
                <div class="modal-body">
                    <p class="modal-message">{{ modalMessage }}</p>
                </div>
                <div class="modal-footer">
                    <button class="btn-cancel" @click="cancel">{{ cancelText }}</button>
                    <button class="btn-confirm" @click="confirm">{{ confirmText }}</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 确认弹窗插件
        const ConfirmPlugin = {
            install(Vue) {
                Vue.prototype.$confirm = function(options) {
                    return new Promise((resolve, reject) => {
                        const vm = this;
                        vm.$set(vm, 'showModal', true);
                        vm.$set(vm, 'modalTitle', options.title || '确认操作');
                        vm.$set(vm, 'modalMessage', options.message || '您确定要执行此操作吗?');
                        vm.$set(vm, 'confirmText', options.confirmText || '确定');
                        vm.$set(vm, 'cancelText', options.cancelText || '取消');
                        
                        // 存储resolve和reject以便在按钮点击时调用
                        vm.$set(vm, 'confirmResolve', resolve);
                        vm.$set(vm, 'confirmReject', reject);
                    });
                };
            }
        };
        
        Vue.use(ConfirmPlugin);
        
        new Vue({
            el: '#app',
            data: {
                showModal: false,
                modalTitle: '确认操作',
                modalMessage: '您确定要执行此操作吗?',
                confirmText: '确定',
                cancelText: '取消',
                lastAction: '',
                confirmResolve: null,
                confirmReject: null
            },
            methods: {
                showDefaultConfirm() {
                    this.$confirm({
                        title: '确认操作',
                        message: '这是一个默认的确认弹窗示例。',
                        confirmText: '继续',
                        cancelText: '取消'
                    }).then(() => {
                        this.lastAction = '用户确认了默认操作';
                    }).catch(() => {
                        this.lastAction = '用户取消了默认操作';
                    });
                },
                
                showDeleteConfirm() {
                    this.$confirm({
                        title: '删除确认',
                        message: '此操作将永久删除该项,且无法恢复。您确定要继续吗?',
                        confirmText: '删除',
                        cancelText: '取消'
                    }).then(() => {
                        this.lastAction = '用户确认删除操作';
                    }).catch(() => {
                        this.lastAction = '用户取消了删除操作';
                    });
                },
                
                showWarningConfirm() {
                    this.$confirm({
                        title: '警告',
                        message: '此操作可能会导致系统异常,建议在非工作时间执行。您确定要继续吗?',
                        confirmText: '继续',
                        cancelText: '取消'
                    }).then(() => {
                        this.lastAction = '用户确认了警告操作';
                    }).catch(() => {
                        this.lastAction = '用户取消了警告操作';
                    });
                },
                
                showCustomConfirm() {
                    this.$confirm({
                        title: '自定义确认',
                        message: '这是一个完全自定义的确认弹窗,您可以修改标题、内容和按钮文字。',
                        confirmText: '我确定',
                        cancelText: '再想想'
                    }).then(() => {
                        this.lastAction = '用户确认了自定义操作';
                    }).catch(() => {
                        this.lastAction = '用户取消了自定义操作';
                    });
                },
                
                closeModal() {
                    this.showModal = false;
                    if (this.confirmReject) {
                        this.confirmReject();
                    }
                },
                
                cancel() {
                    this.showModal = false;
                    if (this.confirmReject) {
                        this.confirmReject();
                    }
                },
                
                confirm() {
                    this.showModal = false;
                    if (this.confirmResolve) {
                        this.confirmResolve();
                    }
                }
            }
        });
    </script>
</body>
</html>

功能说明

这个Vue确认弹窗具有以下特点:

使用方法

在Vue组件中,可以通过以下方式调用确认弹窗:

this.$confirm({
    title: '确认操作',
    message: '您确定要执行此操作吗?',
    confirmText: '确定',
    cancelText: '取消'
}).then(() => {
    // 用户点击确定
    console.log('操作已确认');
}).catch(() => {
    // 用户点击取消
    console.log('操作已取消');
});

这个实现既美观又实用,可以满足大多数确认操作的需求。

总结

到此这篇关于vue弹出是否继续操作的文章就介绍到这了,更多相关vue弹出是否继续操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文