Browse Source

fix(Menu): 修复MenuItem 无法销毁问题 #1788 (#1857)

大胡子 3 years ago
parent
commit
da6ea62649

+ 15 - 0
src/packages/__VUE/menu/index.taro.vue

@@ -81,10 +81,25 @@ export default create({
           }
         };
 
+        const removeLink = (child: any) => {
+          if (child.proxy) {
+            let internalIndex = internalChildren.indexOf(child);
+            if (internalIndex > -1) {
+              internalChildren.splice(internalIndex, 1);
+            }
+
+            let publicIndex = publicChildren.indexOf(child.proxy);
+            if (internalIndex > -1) {
+              publicChildren.splice(publicIndex, 1);
+            }
+          }
+        };
+
         provide(
           'menuParent',
           Object.assign(
             {
+              removeLink,
               link,
               children: publicChildren,
               internalChildren

+ 15 - 0
src/packages/__VUE/menu/index.vue

@@ -79,10 +79,25 @@ export default create({
           }
         };
 
+        const removeLink = (child: any) => {
+          if (child.proxy) {
+            let internalIndex = internalChildren.indexOf(child);
+            if (internalIndex > -1) {
+              internalChildren.splice(internalIndex, 1);
+            }
+
+            let publicIndex = publicChildren.indexOf(child.proxy);
+            if (internalIndex > -1) {
+              publicChildren.splice(publicIndex, 1);
+            }
+          }
+        };
+
         provide(
           'menuParent',
           Object.assign(
             {
+              removeLink,
               link,
               children: publicChildren,
               internalChildren

+ 1 - 15
src/packages/__VUE/menuitem/index.scss

@@ -36,24 +36,10 @@
   }
 }
 
-.menu-item__pop-container {
-  position: fixed;
-  top: 0;
-  bottom: 0;
-  right: 0;
-  left: 0;
-  overflow: hidden;
-}
-
 .nut-menu__pop {
   transition: all 0 ease 0;
   transform: none;
-  position: absolute !important;
-}
-
-.nut-menu__overlay {
-  position: absolute !important;
-  top: auto;
+  overflow: hidden;
 }
 
 .placeholder-element {

+ 11 - 5
src/packages/__VUE/menuitem/index.taro.vue

@@ -9,8 +9,10 @@
     >
     </div>
     <nut-popup
-      class="menu-item__pop-container"
-      :containerStyle="
+      :style="
+        parent.props.direction === 'down' ? { top: parent.offset.value + 'px' } : { bottom: parent.offset.value + 'px' }
+      "
+      :overlay-style="
         parent.props.direction === 'down' ? { top: parent.offset.value + 'px' } : { bottom: parent.offset.value + 'px' }
       "
       v-bind="$attrs"
@@ -18,7 +20,6 @@
       :position="parent.props.direction === 'down' ? 'top' : 'bottom'"
       :duration="parent.props.duration"
       pop-class="nut-menu__pop"
-      overlayClass="nut-menu__overlay"
       :overlay="parent.props.overlay"
       :lockScroll="parent.props.lockScroll"
       @closed="handleClose"
@@ -60,7 +61,7 @@
   </view>
 </template>
 <script lang="ts">
-import { reactive, PropType, inject, getCurrentInstance, computed } from 'vue';
+import { reactive, PropType, inject, getCurrentInstance, computed, onUnmounted } from 'vue';
 import { createComponent } from '@/packages/utils/create';
 const { componentName, create } = createComponent('menu-item');
 import Icon from '../icon/index.taro.vue';
@@ -113,11 +114,16 @@ export default create({
         // 获取子组件自己的实例
         const instance = getCurrentInstance()!;
 
-        const { link } = parent;
+        const { link, removeLink } = parent;
 
         // @ts-ignore
         link(instance);
 
+        onUnmounted(() => {
+          // @ts-ignore
+          removeLink(instance);
+        });
+
         return {
           parent
         };

+ 7 - 2
src/packages/__VUE/menuitem/index.vue

@@ -55,7 +55,7 @@
   </view>
 </template>
 <script lang="ts">
-import { reactive, PropType, inject, getCurrentInstance, computed } from 'vue';
+import { reactive, PropType, inject, getCurrentInstance, computed, onUnmounted } from 'vue';
 import { createComponent } from '@/packages/utils/create';
 const { componentName, create } = createComponent('menu-item');
 import Icon from '../icon/index.vue';
@@ -104,11 +104,16 @@ export default create({
         // 获取子组件自己的实例
         const instance = getCurrentInstance()!;
 
-        const { link } = parent;
+        const { link, removeLink } = parent;
 
         // @ts-ignore
         link(instance);
 
+        onUnmounted(() => {
+          // @ts-ignore
+          removeLink(instance);
+        });
+
         return {
           parent
         };