首页 > 其他分享 >微前端 micro-app 传参、通信

微前端 micro-app 传参、通信

时间:2023-11-16 10:23:00浏览次数:29  
标签:传参 __ app eventCenterForAppNameVite micro router path data

微前端 micro-app 传参、通信

 环境及配置,参考:https://www.cnblogs.com/1285026182YUAN/p/17828681.html

 1. 应用传参

主应用:

<template>
  <div class="container"><micro-app
      name="my-app-page1"
      url="http://localhost:5173/stand"
      :data="microAppData"
      disableSandbox
      inline
    ></micro-app>
  </div>
</template>

<script>
export default {
  data() {
    return {
      microAppData: { msg: '来自基座的数据' },
    };
  },
};

 

 子应用:

创建通信组件 src / utils / MicroTidings.ts 

import { createRouter, createWebHashHistory, RouterHistory, Router } from 'vue-router'


declare global {
  interface Window {
    eventCenterForAppNameVite: any
    __MICRO_APP_NAME__: string
    __MICRO_APP_ENVIRONMENT__: string
    __MICRO_APP_BASE_APPLICATION__: string
  }
}

// 与基座进行数据交互
export function handleMicroData (router: Router) {
    // eventCenterForAppNameVite 是基座添加到window的数据通信对象
    if (window.eventCenterForAppNameVite) {
      // 主动获取基座下发的数据
      console.log('child-vite getData:', window.eventCenterForAppNameVite.getData())
  
      // 监听基座下发的数据变化
      window.eventCenterForAppNameVite.addDataListener((data: Record<string, unknown>) => {
        console.log('child-vite addDataListener:', data)
  
        if (data.path && typeof data.path === 'string') {
          data.path = data.path.replace(/^#/, '')
          // 当基座下发path时进行跳转
          if (data.path && data.path !== router.currentRoute.value.path) {
            router.push(data.path as string)
          }
        }
      })
  
      // 向基座发送数据
      setTimeout(() => {
        window.eventCenterForAppNameVite.dispatch({ myname: 'child-vite' })
      }, 3000)
    }
  }
  

   

 

 

在main.ts中,调用通信方法

import { handleMicroData } from "./utils/MicroTidings";

const app = createApp(App);

app.use(router);
app.use(createPinia());
app.use(ElementPlus);
app.use(http);
app.mount("#my-vite-app");

handleMicroData(router);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

end.

 

标签:传参,__,app,eventCenterForAppNameVite,micro,router,path,data
From: https://www.cnblogs.com/1285026182YUAN/p/17835601.html

相关文章

  • 82岁老人,6个月自学编程,上架自己开发的App!
    82岁老人,6个月自学编程,上架自己开发的App!核桃编程营 ​关注 大部分人在二三十岁上就死去了,过了这个年龄,他们只是自己的影子。此后的余生则是在模仿自己中度过。日复一日,更机械,更装腔作势地重复有生之年的所思所想,所爱所恨。——罗曼·罗兰而有的人,在人生黄......
  • abc327F - Apples(线段树)
    https://atcoder.jp/contests/abc327/tasks/abc327_f我们将时间看作x轴,位置看作y轴,那么我们随着时间增加,维护新加的点对区间的贡献,同时减去过时的点,线段树区间加法维护最大值即可。#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<map>#incl......
  • Vulnhub靶场--NAPPING: 1.0.1
    环境配置靶机连接攻击者主机IP:192.168.47.145目标主机IP:192.168.47.13信息搜集扫描目标主机,发现目标主机开放了22和80端口┌──(kali㉿kali)-[~]└─$sudonmap-sV-sT-A-p-172.18.53.13[sudo]passwordforkali:StartingNmap7.94(https://nmap.org)at202......
  • Microservice - Hexagonal Architecture
    Hexagonalarchitecture(https://alistair.cockburn.us/hexagonal-architecture/),proposedbyAlistairCockburnin2005,isanarchitecturalpatternthataimstobuild looselycoupledapplicationcomponentsthatcanbeconnectedviaportsand adapters.Inth......
  • KET.Application 报错:pywintypes.com_error: (-2147221005, '无效的类字符串', None,
    电脑突然有一天调用KET.Application总是报错,代码:importwin32com.clientxcl=win32com.client.DispatchEx("KET.Application")xcl.Quit()报错:pywintypes.com_error:(-2147221005,'无效的类字符串',None,None)WIN11系统,最新版WPSAI,之前用的好好的,不知道最近卸载了......
  • 【iOS逆向与安全】某茅台App算法分析还原
    1.目标某茅台软件的actParam算法分析还原。2.使用工具mac系统frida-ios-dump:砸壳已越狱iOS设备:脱壳及frida调试IDAPro:静态分析Charles:抓包工具ss:小火箭,配合Charles使用3.流程处理启动闪退在IDAPro搜索SVC得到如下函数列表:NOP掉sub_函数的最后一行汇编......
  • 如何用SaleSmartly集成WhatsApp账号(内含WhatsApp个人号、商业号、API号对比图)
    用SaleSmartly集成WhatsApp账号如果企业有多个WhatsApp账号,无论是个人账号还是工作账号,员工操作起来可能会觉得难以管理和切换。SaleSmartly就可以解决这个问题,让员工在一个平台上同时使用多个WhatsApp账号,不需要频繁地登录和退出,方便客服查看和回复所有的消息和通话。在SaleSmartl......
  • SQLSERVER 2016新建订阅,连接发布服务器时报错“设置连接字符串后无法更改或读取属性 P
    被此问题困扰许久,查遍全网也没有查到合适的解决方案。特此记录,希望帮到需要的人。SQLSERVER2016新建订阅,连接发布服务器时报错“设置连接字符串后无法更改或读取属性Password。(Microsoft.SqlServer.ConnectionInfo)”===================================设置连接字符串......
  • WhatsApp个人号、Business号、API号到底有什么区别
    WhatsApp作为全球苹果应用商店中用户下载量最多的社交网络应用,在全球有25亿的用户,每日活跃人数超过5亿人。很多人都以为WhatsApp就是一个软件,但是其实它是个家族,里面共有三个成员,分别是WhatsApp Messenger,WhatsApp Business和WhatsApp Business API。按照它们推出市场的时间来......
  • mkfs.xfs报错 mkfs.xfs: /dev/new/new_box appears to contain an existing fil
    在设置逻辑卷文件类型时候报错mkfs.xfs:/dev/new/new_boxappearstocontainanexistingfilesystem(ext4).mkfs.xfs:Usethe-foptiontoforceoverwrite.上面是说目标分区,已经存在一个文件系统但是我们有很需要他更改文件系统的话就加一个-f选项[root@server~]......