From 01eb0d07d36239adfd7a96aa33c0604c2d800b26 Mon Sep 17 00:00:00 2001 From: dolymood Date: Tue, 21 Aug 2018 22:27:25 +0800 Subject: [PATCH] fix(create-api): do not cancel watchers on create --- src/common/helpers/create-api-component.js | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/common/helpers/create-api-component.js b/src/common/helpers/create-api-component.js index 06eabbe1..9c6499f1 100644 --- a/src/common/helpers/create-api-component.js +++ b/src/common/helpers/create-api-component.js @@ -84,8 +84,6 @@ export default function createAPIComponent(Vue, Component, events = [], single = renderFn = null } - cancelWatchProps() - // to get Vue options // store router i18n ... const options = { @@ -106,19 +104,21 @@ export default function createAPIComponent(Vue, Component, events = [], single = const eventBeforeDestroy = 'hook:beforeDestroy' - const component = api.open(renderData, renderFn, options) + let component = api.open(renderData, renderFn, options) let oldOwnerInstance = component.__cube__parent if (oldOwnerInstance !== ownerInstance) { if (oldOwnerInstance) { + cancelWatchProps(oldOwnerInstance) oldOwnerInstance.$off(eventBeforeDestroy, oldOwnerInstance.__cube_destroy_handler) oldOwnerInstance.__cube_destroy_handler = null } oldOwnerInstance = component.__cube__parent = ownerInstance const beforeDestroy = function () { - cancelWatchProps() + cancelWatchProps(ownerInstance) if (oldOwnerInstance === ownerInstance) { component.remove() oldOwnerInstance = component.__cube__parent = null + component = null } ownerInstance.$off(eventBeforeDestroy, beforeDestroy) } @@ -148,17 +148,16 @@ export default function createAPIComponent(Vue, Component, events = [], single = } }) if (isInVueInstance) { - ownerInstance.__createAPI_watchers.push( - ownerInstance.$watch(function () { - const props = {} - watchKeys.forEach((key, i) => { - props[key] = ownerInstance[watchPropKeys[i]] - }) - return props - }, function (newProps) { - component && component.$updateProps(newProps) + const cancelWatcher = ownerInstance.$watch(function () { + const props = {} + watchKeys.forEach((key, i) => { + props[key] = ownerInstance[watchPropKeys[i]] }) - ) + return props + }, function (newProps) { + component && component.$updateProps(newProps) + }) + ownerInstance.__createAPI_watchers.push(cancelWatcher) } } } @@ -188,7 +187,7 @@ export default function createAPIComponent(Vue, Component, events = [], single = }) } - function cancelWatchProps() { + function cancelWatchProps(ownerInstance) { if (ownerInstance.__createAPI_watchers) { ownerInstance.__createAPI_watchers.forEach((watcher) => { watcher() -- GitLab