@@ -12,75 +12,307 @@ yarn add @codingapi/ui-framework
12
12
13
13
## 使用
14
14
15
- ### 基本使用
15
+ ### 组件总线
16
16
17
17
``` javascript
18
- import { ComponentBus , EventBus } from ' @codingapi/ui-framework' ;
18
+ import React from " react" ;
19
+ import Space from " @/components/Space" ;
20
+ import {ComponentBus } from " @codingapi/ui-framework" ;
19
21
20
- // 初始化组件总线
21
- const componentBus = new ComponentBus ();
22
22
23
- // 初始化事件总线
24
- const eventBus = new EventBus ();
23
+ const MyComponent = () => {
24
+ return (
25
+ < div>
26
+ my component
27
+ < / div>
28
+ )
29
+ }
25
30
26
- // 注册组件
27
- componentBus .register (' MyComponent' , {
28
- render : () => < div> Hello World< / div>
29
- });
31
+ const ComponentBusTest = () => {
30
32
31
- // 使用组件
32
- const MyComponent = componentBus .get (' MyComponent' );
33
+ const [pageVersion , setPageVersion ] = React .useState (0 );
34
+ const COMPONENT_KEY = ' MyComponent' ;
35
+
36
+ const MyComponentContent = ComponentBus .getInstance ().getComponent (COMPONENT_KEY );
37
+
38
+ const handlerAddComponent = () => {
39
+ ComponentBus .getInstance ().registerComponent (COMPONENT_KEY , MyComponent);
40
+ setPageVersion (Math .random ());
41
+ }
42
+
43
+ const handlerRemoveComponent = () => {
44
+ ComponentBus .getInstance ().removeComponent (COMPONENT_KEY );
45
+ setPageVersion (Math .random ());
46
+ }
47
+
48
+ return (
49
+ <>
50
+ < div
51
+ style= {{
52
+ textAlign: ' center'
53
+ }}
54
+ >
55
+ < h1> ComponentBus Test < / h1>
56
+ < / div>
57
+
58
+ < Space>
59
+ Component:
60
+ {MyComponentContent && (
61
+ < MyComponentContent/ >
62
+ )}
63
+ < button onClick= {handlerAddComponent}> add component< / button>
64
+
65
+ < button onClick= {handlerRemoveComponent}> remove component< / button>
66
+ < / Space>
67
+
68
+ < / >
69
+ )
70
+ }
71
+
72
+ export default ComponentBusTest ;
33
73
```
34
74
35
75
### 事件总线使用
36
76
37
77
``` javascript
38
- // 订阅事件
39
- eventBus .subscribe (' eventName' , (data ) => {
40
- console .log (' Received event:' , data);
41
- });
78
+ import React from " react" ;
79
+ import Space from " @/components/Space" ;
80
+ import {EventBus } from " @codingapi/ui-framework" ;
81
+
82
+ const EventBusTest = () => {
83
+
84
+ const handlerAddEvent = () => {
85
+ EventBus .getInstance ().on (' test' , (data : any ) => {
86
+ alert (data);
87
+ });
88
+ }
89
+
90
+ const handlerRemoveEvent = () => {
91
+ EventBus .getInstance ().off (' test' );
92
+ }
93
+
94
+ const handlerEmitEvent = () => {
95
+ EventBus .getInstance ().emit (' test' , ' test event' );
96
+ }
97
+
42
98
43
- // 发布事件
44
- eventBus .publish (' eventName' , { message: ' Hello' });
99
+ return (
100
+ <>
101
+ < div
102
+ style= {{
103
+ textAlign: ' center'
104
+ }}
105
+ >
106
+ < h1> EventBus Test < / h1>
107
+ < / div>
108
+
109
+ < Space>
110
+ < button onClick= {handlerAddEvent}> add event < / button>
111
+
112
+ < button onClick= {handlerEmitEvent}> emit event < / button>
113
+
114
+ < button onClick= {handlerRemoveEvent}> remove event < / button>
115
+ < / Space>
116
+ < / >
117
+ )
118
+ }
119
+
120
+ export default EventBusTest ;
45
121
```
46
122
47
123
### 访问控制
48
124
49
125
``` javascript
50
- import { Access } from ' @codingapi/ui-framework' ;
126
+ import React from " react" ;
127
+ import {Access } from " @codingapi/ui-framework" ;
128
+ import Space from " ./Space" ;
51
129
52
- // 创建访问控制实例
53
- const access = new Access ();
130
+ const AccessTest = () => {
54
131
55
- // 设置权限
56
- access .setPermission (' user' , [' read' , ' write' ]);
132
+ const [pageVersion , setPageVersion ] = React .useState (Math .random ());
133
+ const handlerAddRole = (role : string ) => {
134
+ const authorities = localStorage .getItem (' authorities' );
135
+ localStorage .setItem (' authorities' , JSON .stringify ([... (authorities ? JSON .parse (authorities) : []), role]));
136
+ setPageVersion (Math .random ());
137
+ }
57
138
58
- // 检查权限
59
- const hasPermission = access .checkPermission (' user' , ' read' );
60
- ```
139
+ const handlerRemoveRole = (role : string ) => {
140
+ const authorities = localStorage .getItem (' authorities' ) || ' []' ;
141
+ localStorage .setItem (' authorities' , JSON .stringify (JSON .parse (authorities).filter ((item : string ) => item !== role)));
142
+ setPageVersion (Math .random ());
143
+ }
144
+
145
+ const handlerRemoveAllRole = () => {
146
+ localStorage .removeItem (' authorities' );
147
+ setPageVersion (Math .random ());
148
+ }
149
+
150
+ return (
151
+ <>
152
+ < div
153
+ style= {{
154
+ textAlign: ' center'
155
+ }}
156
+ >
157
+ < h1> Access Role Test < / h1>
158
+ < / div>
159
+ < Space>
160
+ hasRoles[' admin' ]:
161
+ < Access hasRoles= {[' admin' ]}>
162
+ < div> has admin role< / div>
163
+ < / Access>
164
+
165
+ < button onClick= {() => {
166
+ handlerAddRole (' admin' );
167
+ }}> add admin role
168
+ < / button>
169
+
170
+ < button onClick= {() => {
171
+ handlerRemoveRole (' admin' );
172
+ }}> remove admin role
173
+ < / button>
174
+ < / Space>
175
+
176
+ < Space>
177
+ HasAnyRoles[' user' ]:
178
+ < Access hasAnyRoles= {[' user' ]}>
179
+ < div> has user role< / div>
180
+ < / Access>
181
+
182
+ < button onClick= {() => {
183
+ handlerAddRole (' user' );
184
+ }}> add user role
185
+ < / button>
186
+
187
+ < button onClick= {() => {
188
+ handlerRemoveRole (' user' );
189
+ }}> remove user role
190
+ < / button>
191
+ < / Space>
192
+
193
+
194
+ < Space>
195
+ noAnyRoles[' user' ]:
196
+ < Access noAnyRoles= {[' user' ]}>
197
+ < div> has user role< / div>
198
+ < / Access>
199
+
200
+ < button onClick= {() => {
201
+ handlerAddRole (' user' );
202
+ }}> add user role
203
+ < / button>
61
204
62
- ## Webpack 5 配置
205
+ < button onClick= {() => {
206
+ handlerRemoveRole (' user' );
207
+ }}> remove user role
208
+ < / button>
209
+ < / Space>
63
210
64
- 如果你使用的是 Webpack 5,需要在你的 webpack 配置中添加以下配置:
211
+ < Space>
212
+ isNotRoles:
213
+ < Access isNotRoles= {true }>
214
+ < div> no role< / div>
215
+ < / Access>
216
+ < button onClick= {handlerRemoveAllRole}> remove all role< / button>
217
+ < / Space>
218
+ < / >
219
+ )
220
+ }
221
+
222
+ export default AccessTest ;
223
+ ```
224
+
225
+ ### 微前端动态组件
65
226
66
227
``` javascript
67
- module .exports = {
68
- // ... 其他配置
69
- resolve: {
70
- fallback: {
71
- " util" : false
228
+ import React from " react" ;
229
+ import Space from " @/components/Space" ;
230
+ import {DynamicComponentUtils } from " @codingapi/ui-framework" ;
231
+
232
+
233
+ const MicroComponentTest = () => {
234
+
235
+ const [url , setUrl ] = React .useState (' http://localhost:3000/remoteEntry.js' );
236
+ const [scope , setScope ] = React .useState (' MircoApp' );
237
+ const [module , setModule ] = React .useState (' ./Header' );
238
+
239
+ const [RemoteComponent , setRemoteComponent ] = React .useState < React .ComponentType | null > (null );
240
+
241
+ const handlerAddComponent = () => {
242
+ DynamicComponentUtils .loadRemoteScript (url)
243
+ .then (() => {
244
+ DynamicComponentUtils .loadRemoteComponent (scope, module )
245
+ .then ((ComponentModule : any ) => {
246
+ const Component = ComponentModule .default || ComponentModule;
247
+ setRemoteComponent (() => Component);
248
+ })
249
+ .catch (e => {
250
+ console .log (e);
251
+ });
252
+ })
253
+ .catch (e => {
254
+ console .log (e);
255
+ });
256
+ }
257
+
258
+ const handlerRemoveComponent = () => {
259
+ setRemoteComponent (() => null );
72
260
}
73
- }
74
- };
261
+
262
+ return (
263
+ <>
264
+ < div
265
+ style= {{
266
+ textAlign: ' center'
267
+ }}
268
+ >
269
+ < h1> Load Micro Component Test < / h1>
270
+ < / div>
271
+ < Space>
272
+ < div>
273
+ url:
274
+ < input
275
+ value= {url}
276
+ onChange= {(e ) => {
277
+ setUrl (e .target .value );
278
+ }}/ >
279
+ < / div>
280
+ < div>
281
+ scope:
282
+ < input
283
+ value= {scope}
284
+ onChange= {(e ) => {
285
+ setScope (e .target .value );
286
+ }}/ >
287
+ < / div>
288
+ < div>
289
+ module:
290
+ < input
291
+ value= {module }
292
+ onChange= {(e ) => {
293
+ setModule (e .target .value );
294
+ }}/ >
295
+ < / div>
296
+ < / Space>
297
+ < Space>
298
+ < button onClick= {handlerAddComponent}> add remote component< / button>
299
+ < button onClick= {handlerRemoveComponent}> remove remote component< / button>
300
+ < / Space>
301
+ {RemoteComponent && < RemoteComponent/ > }
302
+ < / >
303
+ )
304
+ }
305
+
306
+ export default MicroComponentTest ;
75
307
```
308
+ 更多实例参考: https://github.com/codingapi/ui-framework/tree/main/playground
76
309
77
310
## 主要特性
78
311
79
312
- 组件总线:用于管理和注册组件
80
313
- 事件总线:用于组件间通信
81
314
- 访问控制:用于权限管理
82
- - TypeScript 支持:提供完整的类型定义
83
- - 模块化:支持按需加载
315
+ - 微前端动态组件:支持动态加载和卸载组件
84
316
85
317
## 开发
86
318
0 commit comments