Skip to content
On this page

SignleSpa 的使用方式

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="/lib/umd/single-spa.js"></script>
    <script>
        singleSpa.registerApplication('app1',
        async () => {
            return {
                    bootstrap:async()=>{
                        console.log('应用启动');
                    },
                    mount:async()=>{
                        console.log('应用挂载');
                    },
                    unmount:async()=>{
                        console.log('应用卸载')
                    }
                }
            },
            location => location.hash.startsWith('#/app1'),
            { store: { name: 'zs' } }
        );
        singleSpa.start();
    </script>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

目录结构

bash
├── index.html # 网页目录
├── lib # 包
│   └── umd
│       └── single-spa.js # 生成的 sps 文件
├── package.json
├── rollup.config.js
└── src
    ├── applications
    │   └── app.js # 放入 registerApplication 方法
    ├── single-spa.js # 导出文件
    └── start.js # 启动
1
2
3
4
5
6
7
8
9
10
11

registerApplication 配置

bash
/src/applications/app.js
1
javascript
const apps = [];

/**
 * @description: 当前注册应用的名字
 * @param {*} appName 应用 名称
 * @param {*} loadApp 加载函数(必须返回的是promise),返回的结果必须包含bootstrap、mount和 unmount做为接入协议
 * @param {*} activeWhen 满足条件时调用loadApp方法
 * @param {*}  customProps 自定义属性可用于父子应用通信
 * @return {*}
 */
export function registerApplication(appName, loadApp, activeWhen, customProps) {
  // 这里讲应用注册好了
  apps.push({
    name: appName,
    loadApp,
    activeWhen, // 当前状态
    customProps,
  });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

single-spa 导出

bash
/src/single-spa.js
1
javascript
export { registerApplication } from "./applications/app.js";
export { start } from "./start.js";
1
2
沪ICP备20006251号-1