Vue.extend
使用基础 Vue 构造器,创建一个子类: 先创建一个类 Sub,接着通过原型继承的方式将该类继承基础 Vue 类,然后给 Sub 类添加一些属性以及将父类的某些属性复制到 Sub 类上,最后将 Sub 类返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 <div id="mount-point" ></div>; var Profile = Vue .extend ({ template : "<p>{{firstName}} {{lastName}} aka {{alias}}</p>" , data : function ( ) { return { firstName : "Walter" , lastName : "White" , alias : "Heisenberg" , }; }, }); new Profile ().$mount("##mount-point" );Vue .extend = function (extendOptions ) { extendOptions = extendOptions || {}; const Super = this ; const SuperId = Super .cid ; const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {}); if (cachedCtors[SuperId ]) { return cachedCtors[SuperId ]; } const name = extendOptions.name || Super .options .name ; if (process.env .NODE_ENV !== "production" && name) { validateComponentName (name); } const Sub = function VueComponent (options ) { this ._init (options); }; Sub .prototype = Object .create (Super .prototype ); Sub .prototype .constructor = Sub ; Sub .cid = cid++; Sub .options = mergeOptions (Super .options , extendOptions); Sub ["super" ] = Super ; if (Sub .options .props ) { initProps (Sub ); } function initProps (Comp ) { const props = Comp .options .props ; for (const key in props) { proxy (Comp .prototype , `_props` , key); } } if (Sub .options .computed ) { initComputed (Sub ); } function initComputed (Comp ) { const computed = Comp .options .computed ; for (const key in computed) { defineComputed (Comp .prototype , key, computed[key]); } } Sub .extend = Super .extend ; Sub .mixin = Super .mixin ; Sub .use = Super .use ; ASSET_TYPES .forEach (function (type ) { Sub [type ] = Super [type ]; }); if (name) { Sub .options .components [name] = Sub ; } Sub .superOptions = Super .options ; Sub .extendOptions = extendOptions; Sub .sealedOptions = extend ({}, Sub .options ); cachedCtors[SuperId ] = Sub ; return Sub ; };
Vue.nextTick
该 API 的原理同实例方法 $nextTick 原理一样,此处不再重复。唯一不同的是实例方法 $nextTick 中回调的 this 绑定在调用它的实例上
Vue.set Vue.delete Vue.directive
注册或获取全局指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 Vue .directive ("my-directive" , { bind : function ( ) {}, inserted : function ( ) {}, update : function ( ) {}, componentUpdated : function ( ) {}, unbind : function ( ) {}, }); Vue .directive ("my-directive" , function ( ) { }); var myDirective = Vue .directive ("my-directive" );Vue .options = Object .create (null );Vue .options ["directives" ] = Object .create (null ); Vue .directive = function (id, definition ) { if (!definition) { return this .options ["directives" ][id]; } else { if (type === "directive" && typeof definition === "function" ) { definition = { bind : definition, update : definition }; } this .options ["directives" ][id] = definition; return definition; } };
Vue.filter
注册或获取全局过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Vue .filter ("my-filter" , function (value ) { }); var myFilter = Vue .filter ("my-filter" );Vue .options = Object .create (null );Vue .options ["filters" ] = Object .create (null );Vue .filter = function (id, definition ) { if (!definition) { return this .options ["filters" ][id]; } else { this .options ["filters" ][id] = definition; return definition; } };
Vue.component
注册或获取全局组件。注册还会自动使用给定的 id 设置组件的名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 Vue .component ( "my-component" , Vue .extend ({ }) ); Vue .component ("my-component" , { }); var MyComponent = Vue .component ("my-component" );Vue .options = Object .create (null );Vue .options ["components" ] = Object .create (null );Vue .filter = function (id, definition ) { if (!definition) { return this .options ["components" ][id]; } else { if (process.env .NODE_ENV !== "production" && type === "component" ) { validateComponentName (id); } if (type === "component" && isPlainObject (definition)) { definition.name = definition.name || id; definition = this .options ._base .extend (definition); } this .options ["components" ][id] = definition; return definition; } };
Vue.use
安装 Vue.js 插件。如果插件是一个对象,必须提供 install 方法。如果插件是一个函数,它会被作为 install 方法。install 方法调用时,会将 Vue 作为参数传入
该方法需要在调用 new Vue() 之前被调用
当 install 方法被同一个插件多次调用,插件将只会被安装一次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Vue .use = function (plugin ) { const installedPlugins = this ._installedPlugins || (this ._installedPlugins = []); if (installedPlugins.indexOf (plugin) > -1 ) { return this ; } const args = toArray (arguments , 1 ); args.unshift (this ); if (typeof plugin.install === "function" ) { plugin.install .apply (plugin, args); } else if (typeof plugin === "function" ) { plugin.apply (null , args); } installedPlugins.push (plugin); return this ; };
Vue.mixin
全局注册一个混入,即可以修改 Vue.options,影响注册之后所有创建的每个 Vue 实例
1 2 3 4 Vue .mixin = function (mixin ) { this .options = mergeOptions (this .options , mixin); return this ; };
Vue.compile
在 render 函数中编译模板字符串。只在独立构建时有效
1 2 3 4 5 6 7 8 9 10 11 12 var res = Vue .compile ("<div><span>{{ msg }}</span></div>" );new Vue ({ data : { msg : "hello" , }, render : res.render , staticRenderFns : res.staticRenderFns , }); Vue .compile = compileToFunctions;
Vue.observable
让一个对象可响应。Vue 内部会用它来处理 data 函数返回的对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 const state = Vue .observable ({ count : 0 });const Demo = { render (h ) { return h ( "button" , { on : { click : () => { state.count ++; }, }, }, `count is: ${state.count} ` ); }, };
Vue.version
提供字符串形式的 Vue 安装版本号。这对社区的插件和组件来说非常有用,你可以根据不同的版本号采取不同的策略
该 API 是在构建时读取了 package.json 中的 version 字段,然后将其赋值给 Vue.version
1 2 3 4 5 6 7 8 9 var version = Number (Vue .version .split ("." )[0 ]);if (version === 2 ) { } else if (version === 1 ) { } else { }