其他分享
首页 > 其他分享> > Vue2 全局注入 过滤器、自定义指令

Vue2 全局注入 过滤器、自定义指令

作者:互联网

过滤器函数
const filterName = (val) => {
  return val      
}

export default { filterName }
  自定义指令函数:

一个指令定义对象可以提供如下几个钩子函数 (均为可选):

我们会在稍后讨论渲染函数时介绍更多 VNodes 的细节。

接下来我们来看一下钩子函数的参数 (即 elbindingvnode 和 oldVnode)

const name = {
    bind: (el, binding, vnode, oldVnode) => {
      ...
    },
    inserted: (el, binding, vnode, oldVnode) => {
      ...
    },
    update: (el, binding, vnode, oldVnode) => {
      ...
    },
    componentUpdated: (el, binding, vnode, oldVnode) => {
      ...
    },
    unbind: (el, binding, vnode, oldVnode) => {
      ...
    }
}

export default { name }
  main.js 注入 Vue对象
// 注册自定义指令
import directives from '@/utils/directives';
// 注册全局过滤器
import filters from '@/utils/filters';

// 全局导入自定义指令
Object.keys(directives).forEach(fncName => {
  Vue.directive(fncName, directives[fncName]);
});

// 全局导入过滤器
Object.keys(filters ).forEach(key => {
    Vue.filter(key, filters [key])
});
 

标签:el,自定义,...,binding,vnode,oldVnode,指令,Vue2,过滤器
来源: https://www.cnblogs.com/fineui/p/15358864.html