Vue3 -- plugin插件

本文详细介绍了如何在Vue3中定义、使用插件,涉及directive指令、mixin混入和config配置。通过实例展示了如何为Vue添加焦点效果、全局行为和配置属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

plugin插件

plugin插件是用来为 Vue 添加全局功能,把通用性的功能进行封装。

定义插件

定义插件需要使用 install 方法。这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象。

        const myPlugin = {
            install(app, options){
               console.log(app, options); 
            }
        }

使用插件

通过全局方法 Vue.use() 使用插件

	pp.use(myPlugin,{name: '张三'})

打开控制台看看app, options分别会输出什么内容。
在这里插入图片描述

从图中可以看出,app包含有directive自定义指令、mixin混入以及config配置等内容,options里面是使用插件传入的{name: ‘张三’}。

添加directive

在plugin插件中添加directive。

 		const myPlugin = {
            install(app, options){
                app.directive("focus",{
                    mounted(el){
                        el.focus();
                    }
                })
            }
        }

输入框获得焦点
在这里插入图片描述

添加mixin

在plugin插件中添加mixin。

 		const myPlugin = {
            install(app, options){
                app.mixin({
                    mounted(){
                        console.log('mixin');
                    }
                })
            }
        }

控制台成功输出:mixin。

在这里插入图片描述

添加config

在plugin插件中添加config。

 		install(app, options){
                app.config.globalProperties.$hello = 'hello';
            }
        }

然后在组件中调用hello,控制台成功打印出hello。
在这里插入图片描述

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue3 -- Plugin插件</title>
    <!-- 使用CDN引入Vue -->
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
    <script>
        const myPlugin = {
            install(app, options){
                // console.log(app, options);
                app.directive("focus",{
                    mounted(el){
                        el.focus();
                    }
                })
                // app.mixin({
                //     mounted(){
                //         console.log('mixin');
                //     }
                // })
                // app.config.globalProperties.$hello = 'hello';
            }
        }
        const app = Vue.createApp({
            mounted(){
                console.log(this.$hello);
            },
            template:`
            <div>
                hello Vue.js!
                <demo />
            </div>`
        });
        app.use(myPlugin,{name: '张三'})
        app.component('demo', {
            template:`<input v-focus/>`
        })
        const vm = app.mount('#root');
    </script>
</body>
</html>

总结

plugin插件:为 Vue 添加全局功能,包括但不限于directive、mixin、config等功能

plugin插件的定义:使用 install 方法定义, install 方法有app, options两个参数

plugin插件的使用:通过全局方法 Vue.use() 使用插件

结语

本小节到此结束,谢谢大家的观看!

如有问题欢迎各位指正

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱划水de鲸鱼哥~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值