vue怎么把api 挂载到全局_Vue全局API简析

本文深入剖析了Vue全局API,包括`Vue.extend`用于创建组件构造函数,以及`Vue.nextTick`在DOM更新后的异步操作。文中详细解释了这两个方法的工作原理,并通过示例展示了它们在实际开发中的应用。此外,还提及了Vue的其他全局方法,如`Vue.set`、`Vue.delete`、`Vue.directive`、`Vue.filter`、`Vue.use`、`Vue.mixin`和`Vue.observable`等。
摘要由CSDN通过智能技术生成

Vue定义了一些API,然后把它放在Vue对象上方便在Vue项目中全局使用

Vue.extend

Vue.extend的参数为一个组件的options,然后返回一个构造函数用于生成新组件。options有哪些属性呢,想象一下平时我们写的Vue组件,主要有template、data、methods还有生命周期函数等等。

demo的话可以查看Vue文档,以下是粘贴的源码。

function initExtend (Vue) {

/**

* Each instance constructor, including Vue, has a unique

* cid. This enables us to create wrapped "child

* constructors" for prototypal inheritance and cache them.

*/

Vue.cid = 0;

var cid = 1;

/**

* Class inheritance

*/

Vue.extend = function (extendOptions) {

extendOptions = extendOptions || {};

var Super = this;

var SuperId = Super.cid;

var cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {});

if (cachedCtors[SuperId]) {

return cachedCtors[SuperId]

}

var name = extendOptions.name || Super.options.name;

if (name) {

validateComponentName(name);

}

var 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;

// For props and computed properties, we define the proxy getters on

// the Vue instances at extension time, on the extended prototype. This

// avoids Object.defineProperty calls for each instance created.

if (Sub.options.props) {

initProps$1(Sub);

}

if (Sub.options.computed) {

initComputed$1(Sub);

}

// allow further extension/mixin/plugin usage

Sub.extend = Super.extend;

Sub.mixin = Super.mixin;

Sub.use 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值