全局API

58 阅读1分钟

Vue.filter

#注册或获取全局过滤器,main.js
Vue.filter( id, [definition] )

#单个使用
// 注册
Vue.filter('my-filter', function (value) {
  // 返回处理后的值
})

// getter,返回已注册的过滤器
var myFilter = Vue.filter('my-filter')

多个filter

filter.js

const levelHander = function(val){
  return val + 10
}

export default{
  levelHander
}

main.js

import filters from './common/js/filter';

Object.keys(filters).forEach(key=>{
  Vue.filter(key,filters[key])
})

index.vue

<span>{{detailData.fatherLevel | levelHander}}</span>

Vue.component

注册或获取全局组件。注册还会自动使用给定的 id 设置组件的名称

globalCompontent.js

import upButton from './../../components/button';

function plugin(Vue) {
  if (plugin.installed) {
    return;
  }
  Vue.component('upButton',upButton)
}

export default plugin;

main.js

import GLCompontent from './common/js/globalCompontent';
Vue.use(GLCompontent)

app.vue

<up-button></up-button>

Vue.extend   
#返回的是一个“扩展实例构造器”。

Vue.nextTick 
#在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

Vue.set  
#设置对象的属性。如果对象是响应式的,确保属性被创建后也是响应式的,同时触发视图更新。这个方法主要用于避开 Vue 不能检测属性被添加的限制。

Vue.delete  
#删除对象的属性。如果对象是响应式的,确保删除能触发更新视图。这个方法主要用于避开 Vue 不能检测到属性被删除的限制,但是你应该很少会使用它。

Vue.directive  
#注册或获取全局指令。



Vue.component
#注册或获取全局组件。注册还会自动使用给定的id设置组件的名称.

Vue.use(plugin)
#使用插件

Vue.mixin( mixin )
#全局注册一个混入,不推荐在应用代码中使用。

Vue.compile( template )
#在 render 函数中编译模板字符串。只在独立构建时有效。

Vue.version
#提供字符串形式的 Vue 安装版本号。