Vue 插件的定义和使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lesson 32</title>
    <script src="https://unpkg.com/vue@next"></script>
    <style>
    </style>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    // plugin插件,也是把通用性的功能封装起来
    const myPlugin = {
        install(app, options) {
            app.provide('name','Dell Lee');
            app.directive('focus', {
                mounted(el) {
                    el.focus();
                }
            });
            app.mixin({
                mounted() {
                    console.log('mixin');
                }
            });
            app.config.globalProperties.$sayHello = 'hello world';
            console.log(app, options);
        }
    };

    const app = Vue.createApp({
        template:
            `<my-test />`
    });

    app.component('my-test',{
        inject:['name'],
        template:
            `<h1>{{name}}</h1>
            <input v-focus />
            `
    });

    app.use(myPlugin, {name:'dell'});

    app.mount('#root');
    
</script>
</html>

插件实现数据校验

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lesson 33</title>
    <script src="https://unpkg.com/vue@next"></script>
    <style>
    </style>
</head>
<body>
    <div id="root"></div>
    <div id="hello"></div>
</body>
<script>
    //对数据做校验的插件
    const validatorPlugin = {
        install(app,options) {
            app.mixin({
                created() {
                    for(let key in this.$options.rules) {
                        const item = this.$options.rules[key];
                        this.$watch(key,(value) => {
                            const result = item.validate(value);
                            if(!result) console.log(item.message)
                        });
                    }
                }
            });
        }
    }

    const app = Vue.createApp({
        data() {
            return { name: 'dell', age:28 }
        },
        rules: {
            age: {
                validate:(age) => {return age > 25},
                message: '太年轻了'
            },
            name: {
                validate: name => name.length >= 4,
                message: 'name too short'
            }
        },
        template:
            `<div>name:{{name}},age:{{age}}</div>`
    });

    // app.mixin({
    //     created() {
    //         for(let key in this.$options.rules) {
    //             const item = this.$options.rules[key];
    //             this.$watch(key,(value) => {
    //                 const result = item.validate(value);
    //                 if(!result) console.log(item.message)
    //             });
    //         }
    //         // console.log(this.$options.rules);
    //     }
    // });

    app.use(validatorPlugin);
    const vm = app.mount('#root');
    
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值