vue自定义属性directives制作Loading/extend制作loading

全局的

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>谷歌炸了</title>
</head>

<body>
    <div id="app">
        <div v-loading="isLoading">
            <div>{{message}}</div>
            <button @click="update">更新</button>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        // 注册一个全局loading的自定义指令
        Vue.directive('loading', {
            update(el, binding, vnode) {
                console.log(el, binding, vnode)
                if (binding.value) {
                    const div = document.createElement('div');
                    div.innerText = "加载中...",
                        div.setAttribute('id', 'loading');
                    div.style.position = 'fixed';
                    div.style.left = 0;
                    div.style.right = 0;
                    div.style.width = '100%';
                    div.style.height = "100%";
                    div.style.display = 'flex';
                    div.style.justifyContent = 'center';
                    div.style.alignItems = "center";
                    div.style.color = "white";
                    div.style.background = "rgba(0,0,0,0.7)";
                    document.body.append(div);
                } else {
                    document.body.removeChild(document.getElementById('loading'))
                }
            }
        })
        
        new Vue({
            el: "#app",
            data: {
                isLoading: false,
                message: ''
            },
            methods: {
                update() {
                    this.isLoading = true;
                    setTimeout(() => {
                        this.message = "更新完成!";
                        this.isLoading = false;
                    }, 3000);
                }
            }
        })
    </script>
</body>

</html>

组件内部的

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div v-loading="isLoading">
            <div>{{message}}</div>
            <button @click="update">更新</button>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                isLoading: false,
                message: ''
            },
            methods: {
                update() {
                    this.isLoading = true;
                    setTimeout(() => {
                        this.message = "更新完成!";
                        this.isLoading = false;
                    }, 3000);
                }
            },
            //组件内部的
            directives: {
                loading: {
                    update(el, binding, vnode) {
                        console.log(el, binding, vnode)
                        if (binding.value) {
                            const div = document.createElement('div');
                            div.innerText = "加载中...",
                                div.setAttribute('id', 'loading');
                            div.style.position = 'fixed';
                            div.style.left = 0;
                            div.style.right = 0;
                            div.style.bottom = 0;
                            div.style.width = '100%';
                            div.style.height = "100%";
                            div.style.display = 'flex';
                            div.style.justifyContent = 'center';
                            div.style.alignItems = "center";
                            div.style.color = "white";
                            div.style.background = "rgba(0,0,0,0.7)";
                            document.body.append(div);
                        } else {
                            document.body.removeChild(document.getElementById('loading'))
                        }
                    }
                }
            }
        })
    </script>
</body>

</html>

在这里插入图片描述

extend制作loading

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>谷歌炸了</title>
    <style>
        #loading-wrapper{
            position: fixed;
            top:0;
            left:0;
            right:0;
            display: flex;
            justify-content: center;
            align-items: center;
            width:100%;
            height:100%;
            background:rgba(0,0,0,0.7);
            color:#fff;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="showLoading">显示Loading</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        // 先通过Vue.extend创建了一个组件
        const LoadingComponent = Vue.extend({
            template: '<div id="loading-wrapper">{{msg}}</div>',
            props: {
                msg: {
                    type: String,
                    default: 'loading...'
                }
            }
            //组件的名称
        }, 'LoadingComponent')
        function loading(msg) {
            console.log(msg)
            const div = document.createElement('div');
            div.setAttribute('id', 'loading-wrapper');
            document.body.append(div);
            // 关键点、自己实例化了一个对象
            new LoadingComponent({
                props: {
                    msg: {
                        type: String,
                        default: msg
                    }
                }
                // 把自己实例化的组件挂载到刚刚创建的div元素上
            }).$mount('#loading-wrapper')
            return ()=>{
                document.body.removeChild(document.getElementById('loading-wrapper'))
            }
        }
        Vue.prototype.$loading=loading
        new Vue({
            el: "#app",
            data: {
                isLoading: false,
                message: ''
            },
            methods: {
                showLoading() {
                    const hide = this.$loading('正在加载中...');
                        setTimeout(() => {
                            hide();
                        }, 100)
                }
            }
        })
    </script>
</body>

</html>

将上面的代码封装成插件

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>谷歌炸了</title>
    <style>
        #loading-wrapper {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="showLoading">显示Loading</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        const loadingPlugin = {
            install: function (vm) {
                // 先通过Vue.extend创建了一个组件
                const LoadingComponent = Vue.extend({
                    template: '<div id="loading-wrapper">{{msg}}</div>',
                    props: {
                        msg: {
                            type: String,
                            default: 'loading...'
                        }
                    }
                    //组件的名称
                }, 'LoadingComponent')
                function loading(msg) {
                    console.log(msg)
                    const div = document.createElement('div');
                    div.setAttribute('id', 'loading-wrapper');
                    document.body.append(div);
                    // 关键点、自己实例化了一个对象
                    new LoadingComponent({
                        props: {
                            msg: {
                                type: String,
                                default: msg
                            }
                        }
                        // 把自己实例化的组件挂载到刚刚创建的div元素上
                    }).$mount('#loading-wrapper')
                    return ()=>{
                        document.body.removeChild(document.getElementById('loading-wrapper'))
                    }
                }
                vm.prototype.$loading=loading
            }
        }
        Vue.use(loadingPlugin)
        new Vue({
            el: "#app",
            data: {
                isLoading: false,
                message: ''
            },
            methods: {
                showLoading() {
                    const hide = this.$loading('正在加载中...');
                    setTimeout(() => {
                        hide();
                    }, 1000)
                }
            }
        })
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值