什么是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的功能的更多相关文章

  1. 三种扩展 Office 软件功能的开发模型对比 – Office Add-In Model, VBA 和 VSTO

    当 Office 用户需要针对文档自定义新功能时,可以求助于 VBA 或者 VSTO 两种方式.Office 2013 富客户端以后,微软为 Office 平台上的开发者提供了一种新模型 --- Of ...

  2. 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段

    创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...

  3. 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments

    使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...

  4. Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试

    Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试   需要FQ才能安装,使用时应该不用FQ了,除非使用postman的历史记录功能:   非常棒的C ...

  5. kindeditor扩展粘贴图片功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  6. kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  7. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

    eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...

  8. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)

    eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...

  9. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

随机推荐

  1. ie和谷歌的兼容性问题

    1.表单的归类 ie下的表单元素在设置了disabled禁用属性之后,在ie下点击,仍然会有焦点.谷歌这是正常的没有焦点 解决方法:给表单元素设置增加属性 unselectable='on'  即可.

  2. 栈 队列 hash表 堆 算法模板和相关题目

    什么是栈(Stack)? 栈(stack)是一种采用后进先出(LIFO,last in first out)策略的抽象数据结构.比如物流装车,后装的货物先卸,先转的货物后卸.栈在数据结构中的地位很重要 ...

  3. <人人都懂设计模式>-装饰模式

    书上,真的用一个人穿衣打拌来讲解装饰模式的呢. from abc import ABCMeta, abstractmethod class Person(metaclass=ABCMeta): def ...

  4. Make Rounddog Happy(2019年杭电多校第十场1011+HDU6701+启发式分治)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 求有多少个子区间满足\(a_l,a_{l+1},\dots,a_r\)均不相同且\(max(a_l,a_{l+1},\dots,a_r)-(r ...

  5. xshell 连接报错 Disconnected from remote host

    Type `help' to learn how to use Xshell prompt. [c:\~]$ Connecting to 20.0.0.91:22...Connection estab ...

  6. 201871010110 - 李华 《面向对象程序设计(java)》第二周学习总结

    第一部分:理论知识学习部分 一.简单的Java程序应运程序 1.标识符0标识符由字母.下划线.美元符号和数字组成,且第一个符号不能为数字.   标识符可用作:类名.对象名.变量名.方法名.数组名.文件 ...

  7. 使用helm管理复杂kubernetes应用

    1. 查看仓库: $ helm repo list NAME URL stable https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/ch ...

  8. Computer-Hunters——项目需求分析

    Computer-Hunters--项目需求分析 前言 本次作业属于2019秋福大软件工程实践Z班 本次作业要求 团队名称: Computer-Hunters 本次作业目标:撰写一份针对团队项目的需求 ...

  9. Qt :编译警告 LNK4042对象被多次指定,已忽略多余的指定 ....segmentLayout.obj

    解决办法: 步骤: 1.将segmentLayout.h .segmentLayout.cpp 从工程中删除(no  permanently delete),并清理工程 2.在代码文件目录重命名seg ...

  10. centos git编译

    1. 下载git源码 https://git-scm.com 2. 根据文档一步步操作 https://git-scm.com/book/en/v2/Getting-Started-Installin ...