Skip to content

Commit

Permalink
fix: invoke component node create hooks before insertion (vuejs#7823)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and hefeng committed Jan 25, 2019
1 parent ab89497 commit 2eab5b9
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 31 deletions.
4 changes: 0 additions & 4 deletions flow/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ declare type InternalComponentOptions = {
_isComponent: true;
parent: Component;
_parentVnode: VNode;
_parentElm: ?Node;
_refElm: ?Node;
render?: Function;
staticRenderFns?: Array<Function>
};
Expand Down Expand Up @@ -81,8 +79,6 @@ declare type ComponentOptions = {
_componentTag: ?string;
_scopeId: ?string;
_base: Class<Component>;
_parentElm: ?Node;
_refElm: ?Node;
};

declare type PropOptions = {
Expand Down
2 changes: 0 additions & 2 deletions src/core/instance/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ export function initInternalComponent (vm: Component, options: InternalComponent
const parentVnode = options._parentVnode
opts.parent = options.parent
opts._parentVnode = parentVnode
opts._parentElm = options._parentElm
opts._refElm = options._refElm

const vnodeComponentOptions = parentVnode.componentOptions
opts.propsData = vnodeComponentOptions.propsData
Expand Down
9 changes: 1 addition & 8 deletions src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
// based on the rendering backend used.
if (!prevVnode) {
// initial render
vm.$el = vm.__patch__(
vm.$el, vnode, hydrating, false /* removeOnly */,
vm.$options._parentElm,
vm.$options._refElm
)
// no need for the ref nodes after initial patch
// this prevents keeping a detached DOM tree in memory (#5851)
vm.$options._parentElm = vm.$options._refElm = null
vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */)
} else {
// updates
vm.$el = vm.__patch__(prevVnode, vnode)
Expand Down
17 changes: 3 additions & 14 deletions src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ import {

// inline hooks to be invoked on component VNodes during patch
const componentVNodeHooks = {
init (
vnode: VNodeWithData,
hydrating: boolean,
parentElm: ?Node,
refElm: ?Node
): ?boolean {
init (vnode: VNodeWithData, hydrating: boolean): ?boolean {
if (
vnode.componentInstance &&
!vnode.componentInstance._isDestroyed &&
Expand All @@ -51,9 +46,7 @@ const componentVNodeHooks = {
} else {
const child = vnode.componentInstance = createComponentInstanceForVnode(
vnode,
activeInstance,
parentElm,
refElm
activeInstance
)
child.$mount(hydrating ? vnode.elm : undefined, hydrating)
}
Expand Down Expand Up @@ -215,15 +208,11 @@ export function createComponent (
export function createComponentInstanceForVnode (
vnode: any, // we know it's MountedComponentVNode but flow doesn't
parent: any, // activeInstance in lifecycle state
parentElm?: ?Node,
refElm?: ?Node
): Component {
const options: InternalComponentOptions = {
_isComponent: true,
parent,
_parentVnode: vnode,
_parentElm: parentElm || null,
_refElm: refElm || null
parent
}
// check inline-template render functions
const inlineTemplate = vnode.data.inlineTemplate
Expand Down
7 changes: 4 additions & 3 deletions src/core/vdom/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ export function createPatchFunction (backend) {
if (isDef(i)) {
const isReactivated = isDef(vnode.componentInstance) && i.keepAlive
if (isDef(i = i.hook) && isDef(i = i.init)) {
i(vnode, false /* hydrating */, parentElm, refElm)
i(vnode, false /* hydrating */)
}
// after calling the init hook, if the vnode is a child component
// it should've created a child instance and mounted it. the child
// component also has set the placeholder vnode's elm.
// in that case we can just return the element and be done.
if (isDef(vnode.componentInstance)) {
initComponent(vnode, insertedVnodeQueue)
insert(parentElm, vnode.elm, refElm)
if (isTrue(isReactivated)) {
reactivateComponent(vnode, insertedVnodeQueue, parentElm, refElm)
}
Expand Down Expand Up @@ -681,7 +682,7 @@ export function createPatchFunction (backend) {
}
}

return function patch (oldVnode, vnode, hydrating, removeOnly, parentElm, refElm) {
return function patch (oldVnode, vnode, hydrating, removeOnly) {
if (isUndef(vnode)) {
if (isDef(oldVnode)) invokeDestroyHook(oldVnode)
return
Expand All @@ -693,7 +694,7 @@ export function createPatchFunction (backend) {
if (isUndef(oldVnode)) {
// empty mount (likely as component), create new root element
isInitialPatch = true
createElm(vnode, insertedVnodeQueue, parentElm, refElm)
createElm(vnode, insertedVnodeQueue)
} else {
const isRealElement = isDef(oldVnode.nodeType)
if (!isRealElement && sameVnode(oldVnode, vnode)) {
Expand Down

0 comments on commit 2eab5b9

Please sign in to comment.