// 初始化实例的时候传入 options 参数 functionVue (options) { if (!(thisinstanceofVue) ) { warn('Vue is a constructor and should be called with the `new` keyword'); } this._init(options); }
// _init 方法 if (options && options._isComponent) { // optimize internal component instantiation // since dynamic options merging is pretty slow, and none of the // internal component options needs special treatment. initInternalComponent(vm, options); } else { vm.$options = mergeOptions( resolveConstructorOptions(vm.constructor), options || {}, vm ); }
// resolveSlots functionresolveSlots ( children, context ) { if (!children || !children.length) { return {} } var slots = {}; for (var i = 0, l = children.length; i < l; i++) { var child = children[i]; var data = child.data; // remove slot attribute if the node is resolved as a Vue slot node if (data && data.attrs && data.attrs.slot) { delete data.attrs.slot; } // named slots should only be respected if the vnode was rendered in the // same context. if ((child.context === context || child.fnContext === context) && data && data.slot != null ) { var name = data.slot; var slot = (slots[name] || (slots[name] = [])); if (child.tag === 'template') { slot.push.apply(slot, child.children || []); } else { slot.push(child); } } else { (slots.default || (slots.default = [])).push(child); } } // ignore slots that contains only whitespace for (var name$1in slots) { if (slots[name$1].every(isWhitespace)) { delete slots[name$1]; } } return slots }
在初始化渲染的时候,解析 DOM 树,通过 resolveSlots 方法 提取出 slots 对象
<p>If I have some content down here, it will also be included in vm.$slots.default.</p>. </blog-post> <script> Vue.component('blog-post', { render: function (createElement) { var header = this.$slots.header var body = this.$slots.default var footer = this.$slots.footer returncreateElement('div', [ createElement('header', header), createElement('main', body), createElement('footer', footer) ]) } }) </script>