创建你自己定制的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 ...
随机推荐
- KL散度=交叉熵-熵
熵:可以表示一个事件A的自信息量,也就是A包含多少信息. KL散度:可以用来表示从事件A的角度来看,事件B有多大不同. 交叉熵:可以用来表示从事件A的角度来看,如何描述事件B. 一种信息论的解释是: ...
- np.append()
1.numpy.append() np.append(arr,values,axis=None) 功能:在array后面追加values 参数: arr:array_like 值将附加到此数组的副本. ...
- Linux之基础系统优化
Linux基础系统优化 >>> https://www.cnblogs.com/pyyu/p/9355477.html Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命 ...
- 码云因为认证失败导致推送失败 生成 SSH 密钥对
- java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact
线上环境中部署的 Tomcat 项目,出现部分页面无法打开的情况,但本地环境是好的.经过排查发现,本地 Tomcat版本为 7.0.77,而线上版本为 7.0.88.报错的具体描述为java.lang ...
- hadoop map中获取文件/切片名称
//import org.apache.hadoop.mapreduce.InputSplit;//import org.apache.hadoop.mapreduce.lib.input.FileS ...
- 请写出css中选择器(元素选择器、类选择器、id选择器)的优先级顺序,和当各种选择器组合时,优先级的计算规则是什么?
id选择器>类选择器>元素选择器 规则:选择器的权重值表述为4个部分,用0,0,0,0表示. 通配符*的权重为0,0,0,0 标签选择器.伪元素选择器的权重为0,0,0,1 类选择器.属性 ...
- PATA1001A+BFormat
这里学到的主要是将数字存储到数组中,倒序输出使用取余10加除10 while(sum) { num[len++] = sum % 10; sum /= 10; } 然后是每三位输出一个逗号,因为是倒序 ...
- 前端,DJ
Vue模块 1.Vue都有哪些指令,简单说说? """ Vue里面常见指令有文本指令:v-text.v-html,属性指令:v-bind,方法指令:v-on,条件指令:v ...
- 箭头函数可不用return直接将表达式作为函数返回值
箭头函数如果函数体只有一个表达式,那么表达式将作为函数的返回值,这种写法无须加上return关键字, 看下面两个函数定义 var testAf = () => '111'; var testAf ...