promise实现公共弹框组件(异步回调)

情景再现:

        1、用户点击按钮,弹出确认窗体

        2、用户确认和取消有不同的处理

解决方案: 

        1、采用ES6的promise语法,实现异步回调(jquery3.0以后支持)

        2、案例样式采用bootstrap

上代码:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>confirm重写</title>
        <link rel="stylesheet" href="bootstrap3.3.5.min.css" />
        <style type="text/css">
            .console-show {
                margin-top: 200px;
                background-color: red;
                color: white;
            }
        </style>
    </head>

    <body>
        <div class="text-center">
            <h1>confirm重写</h1>
            <button type="button" class="btn btn-info" id="confirmBtn">Confirm</button>
            <button type="button" class="btn btn-danger" id="alertBtn">Alert</button>
            <div class="console-show"></div>
        </div>
        <!--下面是模态框-->
        <div class="modal fade" id="alertModal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title"></h4>
                    </div>
                    <div class="modal-body">
                        <p></p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger sureBtn" data-dismiss="modal">确定</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->

        <!--下面是模态框-->
        <div class="modal fade" id="confirmModal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title"></h4>
                    </div>
                    <div class="modal-body">
                        <p></p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default cancelBtn" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-danger sureBtn" data-dismiss="modal">确定</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
    </body>
    <script src="jquery-3.3.1.min.js"></script>
    <script src="bootstrap-3.3.5.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        function ReAlert(title, content) {
            var alertPromise = new Promise(function(resolve, reject){
                $('#alertModal .modal-title').text(title);
                $('#alertModal .modal-body p').text(content);
                $('#alertModal').modal('show');
                $('#alertModal .sureBtn').off('click').click(function() {
                    resolve(content);
                })
            })
            return alertPromise;
        };


        function ReConfirm(title, content) {
            var confirmPromise = new Promise(function(resolve, reject){
                $('#confirmModal .modal-title').text(title);
                $('#confirmModal .modal-body p').text(content);
                $('#confirmModal').modal('show');
                $('#confirmModal .sureBtn').off('click').click(resolve);
                $('#confirmModal .cancelBtn').off('click').click(reject);
            })
            return confirmPromise;
        };

        $('#alertBtn').click(function() {
            ReAlert('提示', '确定加入索尼大法?').then(function(str){
                console.log(str);
                $('.console-show').text('Prmoise_alert页面点击"确定"');
            })
        });

        $('#confirmBtn').click(function() {
            ReConfirm("警告", '确认退出索尼教?').then(function(){
                console.log('确定被点击,执行后续操作');
                $('.console-show').text('Promise_resolve_confirm页面点击"确定"');
            },function(){
                console.log("取消被点击,返回之前的操作");
                $('.console-show').text('Promise_reject_confirm页面点击"取消"');
            })
        })
    </script>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue中可以使用动态组件异步组件实现按需加载和优化性能。下面是两种方式的实现方法。 1. 动态组件 动态组件是通过使用`<component>`标签,并在`is`属性中指定组件名来实现的。动态组件可以在运行时动态绑定组件实现按需加载。 示例代码: ```html <template> <div> <button @click="toggleComponent">Toggle Component</button> <component :is="currentComponent"></component> </div> </template> <script> import ComponentA from './ComponentA.vue'; import ComponentB from './ComponentB.vue'; export default { components: { ComponentA, ComponentB, }, data() { return { currentComponent: 'ComponentA', }; }, methods: { toggleComponent() { this.currentComponent = this.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA'; }, }, }; </script> ``` 2. 异步组件 异步组件是通过使用`Vue.component()`方法来实现的。该方法接收一个函数作为参数,函数返回一个`Promise`对象,在`Promise`对象被解决时,将组件作为参数传递给`Vue.component()`方法。 示例代码: ```html <template> <div> <button @click="loadComponent">Load Component</button> <component v-if="currentComponent" :is="currentComponent"></component> </div> </template> <script> export default { data() { return { currentComponent: null, }; }, methods: { async loadComponent() { const { default: ComponentC } = await import('./ComponentC.vue'); this.currentComponent = ComponentC; }, }, }; </script> ``` 在上面的代码中,我们使用了ES6的动态导入语法来异步加载组件。这将返回一个`Promise`对象,我们可以使用`await`关键字来等待`Promise`对象被解决。在`Promise`对象被解决时,我们将组件作为参数传递给`currentComponent`,并将其渲染到页面上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值