vue过滤器
vue.filter('myFilter',function(val){
return val.split('').reverse().join('')
})
new Vue({el:'#app',data(){
return{
message:'testData'
}
}})
<div id='app'>
<p>{message|myFilter}</p>
</div>
插件
vue.use 内部是调用插件的install方法;比如我们常用的elementUI,其实组件库use方法就是执行了install方法,而install方法内部通过Vue.component(Button.name,Button)声明了对应的组件
import Button from './src/index.vue'
myPlugin.install=function(Vue){
Vue.prototype.$myMethod=function(){
}
Vue.component(Button.name,Button)
}
export default=myPlugin
Vue.use(myPlugin)
自定义指令
类似常用的v-model v-text 等,我们可以扩展开发自定义指令,需要关注的是对应的钩子函数
-
bind
:只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。 -
inserted
:被绑定元素插入父节点时调用 (仅保证父节点存在,但不一定已被插入文档中)。 -
update
:所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。指令的值可能发生了改变,也可能没有。但是你可以通过比较更新前后的值来忽略不必要的模板更新 (详细的钩子函数参数见下)。 -
componentUpdated
:指令所在组件的 VNode 及其子 VNode 全部更新后调用。 -
unbind
:只调用一次,指令与元素解绑时调用。
Vue.directive('focus',{
inserted:function(el){
el.focus()
}
})
<input v-focus>