功能:用于增强Vue

本质:包含install方法的一个对象,install的第一个参数是Vue,第2个以后的参数是插件使用者传递的数据。

1、定义插件:

对象.install = function(Vue,options){

//(1)添加全局过滤器

Vue.filter(...)

//(2)添加全局指令

Vue.directive(...)

//(3)配置全局混入(混合)

Vue.mixin(...)

//(4)添加实例方法

Vue.prototype.$myMethod = function(){...}

Vue.prototype.$myProperty = xxx

}

2、使用插件:Vue.use()

案例:

(1)定义插件

const obj = {
install(Vue){
console.log('@@@ install:'+Vue) //全局过滤器
Vue.filter("mySlice2",function(value){
return value.slice(0,5)
}) //全局自定义指令
Vue.directive("fbind-number",{
//指令与元素成功绑定时调用
bind(element,binding){
element.value = binding.value*10;
},
//指令所在元素被插入页面时调用
inserted(element,binding){
element.focus()
},
//指令所在的模板被重新解析时调用
update(element,binding){
element.value = binding.value*10;
}
}) //全局混入
Vue.mixin({
data(){
return{
x:100,
y:200
}
}
}) //全局方法(给Vue原型添加一个方法)
Vue.prototype.hello = ()=>{
alert("你好啊~")
} }
} export default obj

(2)在main.js 引入插件

//引入插件,应用插件
import plugins from "./plugins/plugins"
Vue.use(plugins)

(3)在组件中应用插件中的东西(过滤器、指令、方法、混入)

<template>
<div class="school">
<h3>学校名:{{SchoolName | mySlice2}}</h3> <!-- 插件中的过滤器 -->
<h3>学校地址:{{address}}</h3>
<input type="text" v-fbind-number="n"><br/> <!-- 插件中的指令 -->
<button @click="hello">点我测试hello方法</button> <!-- 插件中的方法 -->
</div>
</template> <script> export default {
name:"School",
data(){
return{
SchoolName:"尚硅谷123456789",
address:"北京昌平",
n:2
}
}, }
</script>

(4)效果

最新文章

  1. 动作手游实时PVP帧同步方案(客户端)
  2. AD域的安装
  3. VMWare Workstation的命令
  4. 在Excel中创建和使用ServerDocument
  5. hdoj 2191(多重背包)
  6. Starting httpd:Could not reliably determine the server&#39;s fully qualified domain name
  7. asp.net缓存(转载)
  8. 用挂载,使用NTFS移动硬盘,拷贝iPhone里的照片,拷到MAC
  9. angular 4 和django 1.11.1 前后端交互 总结
  10. Android Environment.getExternalStorageDirectory() 获取的是内部存储还是外部存储? - z
  11. Apache 服务器认证 和重写
  12. 2018C语言助教总结
  13. tflearn 实现DNN 全连接
  14. redis集群搭建+lua脚本的使用
  15. 为什么要使用String
  16. JavaScript设计模式-13.组合模式
  17. NEERC2012
  18. 搭建keepalived遇到的问题
  19. MySQL错误代码大全(史上最全)
  20. Unknown column &#39;sid&#39; in &#39;field list&#39;

热门文章

  1. Django框架模板语法传值-过滤器-标签-自定义过滤器,标签,inclusion_tag
  2. redis集合 实现 队列
  3. EXACT函数
  4. 通过rpm安装postgresql-9.6无法远程连接的问题
  5. 宇宙无敌搞笑轻松弄懂java动态代理
  6. final关键字用于修饰成员方法-final关键字用于修饰局部变量
  7. 朋友圈那串神秘字符背后的开源项目「GitHub 热点速览」
  8. Cow Picnic S 更新了(在后面)
  9. npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
  10. 2021级《JAVA语言程序设计》上机考试试题7