// 方式一
var MyComponent = Vue.extend({
name: 'my-component',
template: '<div>A custom component!</div>'
});
Vue.component('my-component', MyComponent);
// 方式二
Vue.component('my-component', {
name: 'my-component',
template: '<div>A custom component!</div>'
});
// 使用组件
<div id="example">
<my-component></my-component>
</div>
Vue.extend = function (extendOptions) {
extendOptions = extendOptions || {};
var Super = this;
var isFirstExtend = Super.cid === 0;
if (isFirstExtend && extendOptions._Ctor) {
return extendOptions._Ctor;
}
var name = extendOptions.name || Super.options.name;
// 如果有name属性,即组件名称,检测name拼写是否合法
if ('development' !== 'production') {
if (!/^[a-zA-Z][\w-]*$/.test(name)) {
warn('Invalid component name: "' + name + '". Component names ' + 'can only contain alphanumeric characaters and the hyphen.');
name = null;
}
}
// 创建一个VueComponent 构造函数,函数名为‘VueComponent'或者name
var Sub = createClass(name || 'VueComponent');
// 构造函数原型继承Vue.prototype
Sub.prototype = Object.create(Super.prototype);
Sub.prototype.constructor = Sub;
Sub.cid = cid++;
// 合并Vue.options和extendOptions,作为新构造函数的静态属性options
Sub.options = mergeOptions(Super.options, extendOptions);
//'super'静态属性指向Vue函数
Sub['super'] = Super;
// start-----------------拷贝Vue静态方法
// allow further extension
Sub.extend = Super.extend;
// create asset registers, so extended classes
// can have their private assets too.
config._assetTypes.forEach(function (type) {
Sub[type] = Super[type];
});
// end-----------------拷贝Vue静态方法
// enable recursive self-lookup
if (name) {
Sub.options.components[name] = Sub;
}
// cache constructor:缓存该构造函数
if (isFirstExtend) {
extendOptions._Ctor = Sub;
}
return Sub;
};
// _assetTypes: ['component', 'directive', 'elementDirective', 'filter', 'transition', 'partial']
config._assetTypes.forEach(function (type) {
// 静态方法Vue.component
Vue[type] = function (id, definition) {
if (!definition) {
return this.options[type + 's'][id];
} else {
/* istanbul ignore if */
if ('development' !== 'production') {
if (type === 'component' && (commonTagRE.test(id) || reservedTagRE.test(id))) {
warn('Do not use built-in or reserved HTML elements as component ' + 'id: ' + id);
}
}
// 如果第二个参数是简单对象,则需要通过Vue.extend创建组件构造函数
if (type === 'component' && isPlainObject(definition)) {
if (!definition.name) {
definition.name = id;
}
definition = Vue.extend(definition);
}
// 将组件函数加入Vue静态属性options.components中,也就是,全局注入该组件
this.options[type + 's'][id] = definition;
return definition;
}
};
});
var MyComponent = Vue.extend({
template: '<div>A custom component!</div>'
});
new Vue({
el: '#example',
components: {
'my-component': MyComponent,
'other-component': {
template: '<div>A custom component!</div>'
}
}
});
Vue.prototype._init = function (options) {
options = options || {};
....
// merge options.
options = this.$options = mergeOptions(this.constructor.options, options, this);
...
};
function mergeOptions(parent, child, vm) {
//解析components属性
guardComponents(child);
guardProps(child);
...
}
function guardComponents(options) {
if (options.components) {
// 将对象转为数组
var components = options.components = guardArrayAssets(options.components);
//ids数组包含组件名
var ids = Object.keys(components);
var def;
if ('development' !== 'production') {
var map = options._componentNameMap = {};
}
// 遍历组件数组
for (var i = 0, l = ids.length; i < l; i++) {
var key = ids[i];
if (commonTagRE.test(key) || reservedTagRE.test(key)) {
'development' !== 'production' && warn('Do not use built-in or reserved HTML elements as component ' + 'id: ' + key);
continue;
}
// record a all lowercase <-> kebab-case mapping for
// possible custom element case error warning
if ('development' !== 'production') {
map[key.replace(/-/g, '').toLowerCase()] = hyphenate(key);
}
def = components[key];
// 如果是组件定义是简单对象-对象字面量,那么需要根据该对象创建组件函数
if (isPlainObject(def)) {
components[key] = Vue.extend(def);
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有