Skip to content

[bug] MenuItem 无法销毁问题 #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vok123 opened this issue Oct 26, 2022 · 4 comments
Closed

[bug] MenuItem 无法销毁问题 #1788

vok123 opened this issue Oct 26, 2022 · 4 comments
Assignees

Comments

@vok123
Copy link

vok123 commented Oct 26, 2022

问题描述

MenuItem 通过响应式控制渲染未被销毁组件

复现步骤,具体代码

<Menu>
  <MenuItem title="Title1" disabled />
  <MenuItem v-if="visible" title="Title2" disabled />
  <MenuItem  title="Title2" disabled />
</Menu>
const visible = ref(true);

setTimeout(() => {
  // 销毁时 Title2 仍存在
  visible.value = false;
}, 1000);

Menu 组件未提供移除 internalChildren publicChildren 中的MenuItem实例函数

Fix code

// fix Menu
const linkChildren = (value?: any) => {
  const link = (child: any) => {
    if (child.proxy) {
      internalChildren.push(child);
      publicChildren.push(child.proxy as any);
    }
  };
  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
      },
      value
    )
  );
};
// fix MenuItem
const useParent: any = () => {
  const parent = inject<any>('menuParent', null);
  if (parent) {
    // 获取子组件自己的实例
    const instance = getCurrentInstance()!;
    const { link, removeLink } = parent;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    link(instance);

    onUnmounted(() => {
      removeLink(instance);
    });

    return {
      parent
    };
  }
};

相关环境信息

  • 操作系统:mac
  • Node 版本:16.0
  • NutUI 版本:3.2.5
  • Vue 版本:3.2.27
  • 引用方式: NPM
@yangjinjun3
Copy link
Contributor

Item 别用自闭合标签试试!

@vok123
Copy link
Author

vok123 commented Nov 30, 2022

Item 别用自闭合标签试试!

闭合与否都是组件, 本身就是没做销毁的处理

@yangjinjun3
Copy link
Contributor

嗯 周五发版修复。感谢反馈

@vok123
Copy link
Author

vok123 commented Dec 2, 2022

嗯 周五发版修复。感谢反馈

谢谢啦

@szg2008 szg2008 closed this as completed Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants