Vue+ElementUI实现分步骤的弹出框

243 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Step Dialog</title>
    <!-- 引入 Vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <!-- 引入 Element UI CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
    <div id="app">
        <el-button @click="showDialog = true">打开对话框</el-button>
        
        <el-dialog :visible.sync="showDialog" title="分步骤对话框">
            <el-steps :active="currentStep" finish-status="success">
                <el-step title="第一步"></el-step>
                <el-step title="第二步"></el-step>
                <el-step title="第三步"></el-step>
            </el-steps>
            
            <template v-if="currentStep === 0">
                这里放置第一步内容...
            </template>
            
            <template v-else-if="currentStep === 1">
                这里放置第二步内容...
            </template>
            
            <template v-else-if="currentStep === 2">
                这里放置第三步内容...
            </template>
            
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="nextStep">下一步</el-button>
                <el-button @click="closeDialog">取消</el-button>
            </span>
        </el-dialog>
    </div>
    
    <!-- 引入 Element UI JS -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    showDialog: false, // 控制对话框显示与否
                    currentStep: 0 // 当前所处步骤
                }
            },
            methods: {
                nextStep() {
                    if (this.currentStep !== 2) {
                        this.currentStep++;
                    } else {
                        // 最后一步完成操作
                        alert('操作已完成!');
                        this.closeDialog();
                    }
                },
                closeDialog() {
                    this.showDialog = false;
                    this.currentStep = 0;
                }
            }
        });
    </script>
</body>
</html>

上述代码通过el-dialog组件创建了一个包含多个步骤的弹出框,每个步骤由el-step组件表示。根据currentStep变量的值,动态展示不同的步骤内容。点击“下一步”按钮时会切换到下一步,直到达到最后一步为止;点击“取消”按钮或关闭对话框都会将其关闭。