Skip to content

Commit 43d4b64

Browse files
committed
fix(menu): 修复MenuItem 无法销毁问题 (jd-opensource#1788)
1 parent de05d02 commit 43d4b64

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/packages/__VUE/menu/index.taro.vue

+15
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,25 @@ export default create({
8181
}
8282
};
8383
84+
const removeLink = (child: any) => {
85+
if (child.proxy) {
86+
let internalIndex = internalChildren.indexOf(child);
87+
if (internalIndex > -1) {
88+
internalChildren.splice(internalIndex, 1);
89+
}
90+
91+
let publicIndex = publicChildren.indexOf(child.proxy);
92+
if (internalIndex > -1) {
93+
publicChildren.splice(publicIndex, 1);
94+
}
95+
}
96+
};
97+
8498
provide(
8599
'menuParent',
86100
Object.assign(
87101
{
102+
removeLink,
88103
link,
89104
children: publicChildren,
90105
internalChildren

src/packages/__VUE/menu/index.vue

+15
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,25 @@ export default create({
7979
}
8080
};
8181
82+
const removeLink = (child: any) => {
83+
if (child.proxy) {
84+
let internalIndex = internalChildren.indexOf(child);
85+
if (internalIndex > -1) {
86+
internalChildren.splice(internalIndex, 1);
87+
}
88+
89+
let publicIndex = publicChildren.indexOf(child.proxy);
90+
if (internalIndex > -1) {
91+
publicChildren.splice(publicIndex, 1);
92+
}
93+
}
94+
};
95+
8296
provide(
8397
'menuParent',
8498
Object.assign(
8599
{
100+
removeLink,
86101
link,
87102
children: publicChildren,
88103
internalChildren

src/packages/__VUE/menuitem/index.taro.vue

+7-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</view>
6161
</template>
6262
<script lang="ts">
63-
import { reactive, PropType, inject, getCurrentInstance, computed } from 'vue';
63+
import { reactive, PropType, inject, getCurrentInstance, computed, onUnmounted } from 'vue';
6464
import { createComponent } from '@/packages/utils/create';
6565
const { componentName, create } = createComponent('menu-item');
6666
import Icon from '../icon/index.taro.vue';
@@ -113,11 +113,16 @@ export default create({
113113
// 获取子组件自己的实例
114114
const instance = getCurrentInstance()!;
115115
116-
const { link } = parent;
116+
const { link, removeLink } = parent;
117117
118118
// @ts-ignore
119119
link(instance);
120120
121+
onUnmounted(() => {
122+
// @ts-ignore
123+
removeLink(instance);
124+
});
125+
121126
return {
122127
parent
123128
};

src/packages/__VUE/menuitem/index.vue

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</view>
5656
</template>
5757
<script lang="ts">
58-
import { reactive, PropType, inject, getCurrentInstance, computed } from 'vue';
58+
import { reactive, PropType, inject, getCurrentInstance, computed, onUnmounted } from 'vue';
5959
import { createComponent } from '@/packages/utils/create';
6060
const { componentName, create } = createComponent('menu-item');
6161
import Icon from '../icon/index.vue';
@@ -104,11 +104,16 @@ export default create({
104104
// 获取子组件自己的实例
105105
const instance = getCurrentInstance()!;
106106
107-
const { link } = parent;
107+
const { link, removeLink } = parent;
108108
109109
// @ts-ignore
110110
link(instance);
111111
112+
onUnmounted(() => {
113+
// @ts-ignore
114+
removeLink(instance);
115+
});
116+
112117
return {
113118
parent
114119
};

0 commit comments

Comments
 (0)