Skip to content
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

vue3 - 小程序 getCurrentInstance().value?.ctx.$children 始终存在销毁节点,导致其length不断增多 #5143

Open
dongjinc opened this issue Sep 11, 2024 · 2 comments
Labels
bug Something isn't working mp mini program uni api

Comments

@dongjinc
Copy link

dongjinc commented Sep 11, 2024

1.发生了什么
instance.value?.ctx.$children 始终存在销毁节点,导致其length不断增多
ex:

<NhForm>
  <NhField></NhField>
  <NhField></NhField>
  <NhField v-if=“条件判断”></NhField>
</NhForm>
<script setup lang="ts">
defineOptions({
  name: "NhForm",
});
const instance = ref(null);

onMounted(() => {
  instance.value = getCurrentInstance();
});

const nhFormRef = ref(null);

// const nhFormInstance = getCurrentInstance();
const validate = () => {
  return new Promise((resolve, reject) => {
    console.log(instance.value?.ctx.$parent.$children.length, 'nhFormRef')
    if (!!instance.value?.ctx.$children.length) {
      const hasInValid = instance.value?.ctx.$children.filter((item: any) => !item.validate());
      resolve(!hasInValid.length);
    } else {
      resolve(true);
    }
  });
};

defineExpose({
  validate,
});
</script>

<template>
  <form class="nh-form" id="id">
    <template>
      <slot /> 
    </template>
  </form>
</template>

通过 instance.value?.ctx.$children 获取 当前 slot 插槽渲染的组件,如果渲染组件时,内有条件组件,会导致这个 children.length 不断增加,比如当前实际3个节点,内部因条件事件导致出现了5个节点,销毁的节点也在。

期望:
instance.value?.ctx.$children 移除销毁节点;

{
    endMeasure(instance, `init`);
  }
  {
    if (options.parentComponent && instance.proxy) {
     // 具体源码问题出现在这里, 只有push,没有移除操作。
      options.parentComponent.ctx.$children.push(getExposeProxy(instance) || instance.proxy);
    }
  }
  setupRenderEffect(instance);
  {
    popWarningContext();
    endMeasure(instance, `mount`);
  }
@dongjinc dongjinc changed the title instance.value?.ctx.$children 始终存在销毁节点,导致其length不断增多 vue3 - 小程序 getCurrentInstance().value?.ctx.$children 始终存在销毁节点,导致其length不断增多 Sep 11, 2024
@bfc846958672
Copy link
Contributor

bfc846958672 commented Sep 11, 2024

已确认bug,下个版本修复。 非常不推荐使用$children api, 这个api 在vue3语法内是没有的,是uniapp内部特殊添加的。你这个需求应该可以通过 provide,inject api来解决

@dongjinc
Copy link
Author

已确认bug,下个版本修复。 非常不推荐使用$children api, 这个api 在vue3语法内是没有的,是uniapp内部特殊添加的。你这个需求应该可以通过 provide,inject api来解决

好的,后面我再改造一下,非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mp mini program uni api
Projects
None yet
Development

No branches or pull requests

2 participants