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

子应用通过dispatch向基座发送数据 #897

Closed
SSQLQIANBB opened this issue Sep 9, 2023 · 1 comment · May be fixed by #1230
Closed

子应用通过dispatch向基座发送数据 #897

SSQLQIANBB opened this issue Sep 9, 2023 · 1 comment · May be fixed by #1230

Comments

@SSQLQIANBB
Copy link

第一次发送数据

window.microApp?.dispatch({
    siderShow: true,
    item: {
        _blank: true,
        customKey: 'goPage',
    },
    appName: 'chartApp',
  });

主应用通过handleDataChange监听数据

<micro-app
    v-if="activeApp"
    :name="activeApp.name"
    :url="activeApp.url"
    :baseroute="activeApp.baseRoute"
    :inline="activeApp.isVite"
    :disable-sandbox="activeApp.isVite"
    :data="microData"
    @created="handleCreate"
    @beforemount="handleBeforeMount"
    @mounted="handleMounted"
    @unmount="handleUnMount"
    @error="handleError"
    @datachange="handleDataChange"
    class="micro-app-container"
  />
  
  function handleDataChange(e: CustomEvent): void {
      console.log('来自子应用 child-vite 的数据:', e.detail?.data);
      const { item, appName } = e.detail?.data;
      // 子应用协作空间   的动态 传递kinds 用于跳转
      if (['teamSpaceApp', 'chartApp'].includes(appName) && item?.customKey == 'goPage') {
        const res = getRoute(item);
        if (res) {
          // _blank打开新窗口
          if (item?._blank) {
            const resolve = router.resolve(res);

            window.open(resolve.href, '_blank');
          } else router.push(res);
        }
      }
    }

主应用接受数据正常

// 接受到的数据
{
    siderShow: true,
    item: {
        _blank: true,
        customKey: 'goPage',
    },
    appName: 'chartApp',
  }

第二次发送数据

window.microApp?.dispatch({
    test: 'test'
  });
// 接受到的数据
{
    test: 'test',
    siderShow: true,
    item: {
        _blank: true,
        customKey: 'goPage',
    },
    appName: 'chartApp',
  }

为什么会把第一次的数据与第二次的数据合并,为什么要这样设计,设计初衷是什么

@tjyuanpeng
Copy link

这个不能当传统的事件系统来使用,其实是用来同步数据的
merge数据为了让你每次得到的数据都是全量
由于只是merge第一层数据,你可以把数据放到event的更深层次结构中,避免被merge
如果可以的话,建议自己实现事件系统

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants