创建你自己定制的vuejs plugin扩展app的功能
什么是vuejs plugin插件
vuejs plugin插件是一个向你的app注入新的全局功能的强大但又简约的方式。从概念上来说,vue plugin非常简单,它就是一个包含了install方法的object.而这个install方法有两个参数会传入,第一个参数为全局的Vue构造函数,第二个参数则是options对象.
你的首个插件(任何组件mounted就自动打印mounted log日志)
我们先写一个简单的vue plugin,实现的功能是每个component,当mounted时就能够打印相应的已加载信息
- // This is your plugin object. It can be exported to be used anywhere.
- const MyPlugin = {
- // The install method is all that needs to exist on the plugin object.
- // It takes the global Vue object as well as user-defined options.
- install(Vue, options) {
- // We call Vue.mixin() here to inject functionality into all components.
- Vue.mixin({
- // Anything added to a mixin will be injected into all components.
- // In this case, the mounted() method runs when the component is added to the DOM.
- mounted() {
- console.log('Mounted!');
- }
- });
- }
- };
- export default MyPlugin;
这个插件本质上做的工作就是通过调用Vue.mixin向Vue全局构造函数中添加相应的mounted hook
随后,我们通过vue.use来调用这个plugin
- import Vue from 'vue'
- import MyPlugin from './my-vue-plugin.js'
- import App from './App.vue'
- // The plugin is loaded here.
- Vue.use(MyPlugin)
- new Vue({
- el: '#app',
- render: h => h(App)
- });
需要注意的是:所有的plugin都必须在调用new Vue()之前被Vue.use来初始化
你可能在疑惑,为什么我不能直接在main.js中调用Vue.mixin()来实现同样的功能呢?很重要的原因是因为我们是向Vue添加全局的functionality,而不是在向app添加功能。
添加其他的功能(installing app-wide components and directives)
比如,如果希望通过plugin方式来打包并且分发components以及directives的话,可以写以下代码:
- import MyComponent from './components/MyComponent.vue';
- import MyDirective from './directives/MyDirective.js';
- const MyPlugin {
- install(Vue, options) {
- Vue.component(MyComponent.name, MyComponent)
- Vue.directive(MyDirective.name, MyDirective)
- }
- };
- export default MyPlugin;
修改全局的Vue构造函数对象
看下面的代码:
- const MyPlugin {
- install(Vue, options) {
- Vue.myAddedProperty = 'Example Property'
- Vue.myAddedMethod = function() {
- return Vue.myAddedProperty
- }
- }
- };
- export default MyPlugin;
修改Vue实例化instance
通过javascript的原型机制我们知道所有的vue component都是Vue构造函数new出来的instance,我们只要修改构造函数的prototype就能对instance统一添加新的功能。
- const MyPlugin {
- install(Vue, options) {
- Vue.prototype.$myAddedProperty = 'Example Property'
- Vue.prototype.$myAddedMethod = function() {
- return this.$myAddedProperty
- }
- }
- };
- export default MyPlugin;
上面添加的所有属性或者方法都将在vue component instance中能够通过this.$myAddedProperty来访问.
添加组件的lifecycle hooks或者属性
- const MyPlugin = {
- install(Vue, options) {
- Vue.mixin({
- mounted() {
- console.log('Mounted!');
- }
- });
- }
- };
- export default MyPlugin;
自动安装
对于那些没有使用webpack等模块打包工具的开发者来说,他们往往会将yourplugin.js放在vuejs的script tag之后来引用,可以通过在my-vue-plugin.js中的以下代码来自动安装
- // Automatic installation if Vue has been added to the global scope.
- if (typeof window !== 'undefined' && window.Vue) {
- window.Vue.use(MyPlugin)
- }
创建你自己定制的vuejs plugin扩展app的功能的更多相关文章
- 三种扩展 Office 软件功能的开发模型对比 – Office Add-In Model, VBA 和 VSTO
当 Office 用户需要针对文档自定义新功能时,可以求助于 VBA 或者 VSTO 两种方式.Office 2013 富客户端以后,微软为 Office 平台上的开发者提供了一种新模型 --- Of ...
- 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段
创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...
- 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments
使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...
- Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试
Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试 需要FQ才能安装,使用时应该不用FQ了,除非使用postman的历史记录功能: 非常棒的C ...
- kindeditor扩展粘贴图片功能&修改图片上传路径并通过webapi上传图片到图片服务器
前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...
- kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器
前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
随机推荐
- python接口自动化17-multipart/form-data表单提交
前言 multipart/form-data这种格式官方文档给的参考案例比较简单,实际情况中遇到会比较复杂,本篇讲解multipart/form-data的表单如何提交,非图片上传 禅道提交bug 1 ...
- VS操作中遇到的问题及解决
1.无法解析的外部符号 _main,该符号在函数 "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) 中被引用 2. /ZI ...
- 使用WIFI准备工作及配置内核——韦东山
主要做的工作:让内核如何支持现有的无线网卡.知道这个流程就可以了,没必要深究. 使用WIFI功能时,涉及两个东西: 同样手机也可以用于WIFI AP模式,让别的设备来连接它.就是我们平时所说的用手机开 ...
- Consul 学习资料
资料 网址 Consul 入门指南 https://book-consul-guide.vnzmi.com/
- Maven 中 dependencyManagement 标签使用
1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...
- js提取DOM属性和设置DOM属性值
<style type="text/css"> #div1{width:100px;height:100px;} #div2{background} </styl ...
- 1-PLC基础入门系列(PLC介绍,连接下载说明)
阅读这节文章之前请先阅读这一篇 https://www.cnblogs.com/yangfengwu/p/7681702.html 首先明确一点,PLC就是用单片机做的,后面我会给大家演示用我自己的 ...
- BILIBILI之滑块验证
bilibili的滑动验证码图片比较好玩,和前一篇不大一样. 采用canvas方法,分析发现只找到一个图片,不过,可以通过设置display截图方式获得2张图(完整图片,带缺口的图片),取得图片后接下 ...
- c# Aes加解密
using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; ...
- Vue实践TS中的一些常见错误解决方案
mixin报错 import { Component, Prop, Vue ,Mixins} from 'vue-property-decorator' import httpminix from ' ...