py-test-component 2.0.6 → 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEVELOPER.md +541 -0
- package/USAGE.md +366 -0
- package/dist/vue.esm.js +2 -0
- package/dist/vue.esm.js.LICENSE.txt +7 -0
- package/dist/vue.js +2 -0
- package/dist/vue.js.LICENSE.txt +7 -0
- package/package.json +61 -59
- package/src/components/PyTable.vue +0 -43
- package/src/components/PyWeather.vue +0 -109
- package/src/index.js +0 -38
- package/src/react/index.js +0 -55
- package/src/store/index.js +0 -75
- package/src/utils/api.js +0 -35
- package/src/utils/request.js +0 -68
- package/src/vue/index.js +0 -12
package/USAGE.md
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
# 使用文档
|
|
2
|
+
|
|
3
|
+
本文档介绍如何在 Vue 和 React 项目中使用 `py-test-component` 组件库。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Vue 项目使用
|
|
8
|
+
|
|
9
|
+
### 1. 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install py-test-component
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 2. 引入 ElementUI CSS
|
|
16
|
+
|
|
17
|
+
在入口文件(如 `main.js`)中引入 ElementUI 样式:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import Vue from 'vue';
|
|
21
|
+
import 'element-ui/lib/theme-chalk/index.css'; // 必须引入
|
|
22
|
+
import { initStore } from 'py-test-component';
|
|
23
|
+
|
|
24
|
+
// 初始化组件库配置(仅设置,配置仅供组件库内部使用)
|
|
25
|
+
initStore({
|
|
26
|
+
apiKey: 'your-api-key',
|
|
27
|
+
baseUrl: 'https://api.example.com'
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**注意**:`initStore` 只能设置配置,无法读取 store 状态,避免与外部工程的状态管理冲突。
|
|
32
|
+
|
|
33
|
+
### 3. 使用组件
|
|
34
|
+
|
|
35
|
+
#### 方式一:按需导入组件
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div>
|
|
40
|
+
<PyTable :propData="tableData" />
|
|
41
|
+
<PyWeather />
|
|
42
|
+
</div>
|
|
43
|
+
</template>
|
|
44
|
+
|
|
45
|
+
<script>
|
|
46
|
+
import { PyTable, PyWeather } from 'py-test-component/vue';
|
|
47
|
+
|
|
48
|
+
export default {
|
|
49
|
+
components: {
|
|
50
|
+
PyTable,
|
|
51
|
+
PyWeather
|
|
52
|
+
},
|
|
53
|
+
data() {
|
|
54
|
+
return {
|
|
55
|
+
tableData: [
|
|
56
|
+
{ date: '2024-01-01', name: '张三', address: '北京市' },
|
|
57
|
+
{ date: '2024-01-02', name: '李四', address: '上海市' }
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
</script>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### 方式二:全局注册
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
// main.js
|
|
69
|
+
import { PyTable, PyWeather } from 'py-test-component/vue';
|
|
70
|
+
|
|
71
|
+
Vue.component('PyTable', PyTable);
|
|
72
|
+
Vue.component('PyWeather', PyWeather);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 4. 组件参数
|
|
76
|
+
|
|
77
|
+
#### PyTable
|
|
78
|
+
|
|
79
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
80
|
+
|------|------|--------|------|
|
|
81
|
+
| propData | any | null | 表格数据,需为数组格式 |
|
|
82
|
+
|
|
83
|
+
```vue
|
|
84
|
+
<PyTable :propData="[
|
|
85
|
+
{ date: '2024-01-01', name: '张三', address: '北京' }
|
|
86
|
+
]" />
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### PyWeather
|
|
90
|
+
|
|
91
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
92
|
+
|------|------|--------|------|
|
|
93
|
+
| propData | object | null | 可选配置,如 `{ city: '北京' }` |
|
|
94
|
+
|
|
95
|
+
```vue
|
|
96
|
+
<PyWeather :propData="{ city: '北京' }" />
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 5. initStore 配置
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
import { initStore } from 'py-test-component';
|
|
103
|
+
|
|
104
|
+
// 同步调用,仅设置配置到组件库内部 store
|
|
105
|
+
// 配置仅供组件库内部使用,外部无法读取
|
|
106
|
+
initStore({
|
|
107
|
+
apiKey: 'your-api-key',
|
|
108
|
+
baseUrl: 'https://api.example.com',
|
|
109
|
+
// 任意自定义配置
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**重要**:`initStore` 只能写入配置,外部工程无法通过任何方式读取 store 状态,确保不会污染外部工程的状态管理。
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## React 项目使用
|
|
118
|
+
|
|
119
|
+
### 1. 安装
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm install py-test-component
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 2. 初始化配置
|
|
126
|
+
|
|
127
|
+
在入口文件(如 `index.js`)中初始化:
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
import React from 'react';
|
|
131
|
+
import ReactDOM from 'react-dom/client';
|
|
132
|
+
import { initStore } from 'py-test-component/react';
|
|
133
|
+
import App from './App';
|
|
134
|
+
|
|
135
|
+
// 异步初始化(返回 Promise,不返回 store)
|
|
136
|
+
initStore({
|
|
137
|
+
apiKey: 'your-api-key',
|
|
138
|
+
baseUrl: 'https://api.example.com'
|
|
139
|
+
}).then(() => {
|
|
140
|
+
console.log('组件库初始化完成');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// 或者使用 async/await
|
|
144
|
+
async function bootstrap() {
|
|
145
|
+
await initStore({ apiKey: 'xxx' });
|
|
146
|
+
|
|
147
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
148
|
+
root.render(<App />);
|
|
149
|
+
}
|
|
150
|
+
bootstrap();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**注意**:`initStore` 返回的 Promise 不会 resolve store 实例,仅表示初始化完成。
|
|
154
|
+
|
|
155
|
+
### 3. 使用组件
|
|
156
|
+
|
|
157
|
+
```jsx
|
|
158
|
+
import React, { useState } from 'react';
|
|
159
|
+
import { PyTable, PyWeather } from 'py-test-component/react';
|
|
160
|
+
|
|
161
|
+
function MyComponent() {
|
|
162
|
+
const [tableData] = useState([
|
|
163
|
+
{ date: '2024-01-01', name: '张三', address: '北京市' },
|
|
164
|
+
{ date: '2024-01-02', name: '李四', address: '上海市' }
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<div>
|
|
169
|
+
<h1>React 中使用 Vue 组件</h1>
|
|
170
|
+
|
|
171
|
+
<section>
|
|
172
|
+
<h2>表格组件</h2>
|
|
173
|
+
<PyTable
|
|
174
|
+
propData={tableData}
|
|
175
|
+
loading={<div>加载中...</div>} // 可选:加载占位
|
|
176
|
+
/>
|
|
177
|
+
</section>
|
|
178
|
+
|
|
179
|
+
<section>
|
|
180
|
+
<h2>天气组件</h2>
|
|
181
|
+
<PyWeather loading={<div>加载中...</div>} />
|
|
182
|
+
</section>
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export default MyComponent;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 4. 组件参数
|
|
191
|
+
|
|
192
|
+
#### PyTable
|
|
193
|
+
|
|
194
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
195
|
+
|------|------|--------|------|
|
|
196
|
+
| propData | any | undefined | 表格数据数组 |
|
|
197
|
+
| loading | ReactNode | undefined | 组件加载时的占位内容 |
|
|
198
|
+
|
|
199
|
+
```jsx
|
|
200
|
+
<PyTable
|
|
201
|
+
propData={[{ date: '2024-01-01', name: '张三' }]}
|
|
202
|
+
loading={<Spinner />}
|
|
203
|
+
/>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### PyWeather
|
|
207
|
+
|
|
208
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
209
|
+
|------|------|--------|------|
|
|
210
|
+
| propData | object | undefined | 可选配置对象 |
|
|
211
|
+
| loading | ReactNode | undefined | 组件加载时的占位内容 |
|
|
212
|
+
|
|
213
|
+
```jsx
|
|
214
|
+
<PyWeather
|
|
215
|
+
propData={{ city: '北京' }}
|
|
216
|
+
loading={<div>正在加载天气组件...</div>}
|
|
217
|
+
/>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### 5. initStore 配置
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
import { initStore } from 'py-test-component/react';
|
|
224
|
+
|
|
225
|
+
// 异步调用,仅设置配置到组件库内部 store
|
|
226
|
+
// 不返回 store 实例,外部无法读取
|
|
227
|
+
await initStore({
|
|
228
|
+
apiKey: 'your-api-key',
|
|
229
|
+
baseUrl: 'https://api.example.com'
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
**重要**:React 中的 `initStore` 是异步的,且不暴露任何 store 方法,配置仅供组件库内部使用。
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Store 使用原则
|
|
238
|
+
|
|
239
|
+
**组件库的 store 完全内部化**:
|
|
240
|
+
- ✅ 外部可以通过 `initStore` 设置配置
|
|
241
|
+
- ❌ 外部无法读取 store 状态
|
|
242
|
+
- ❌ 外部无法订阅 store 变化
|
|
243
|
+
- ❌ 外部无法获取 store 实例
|
|
244
|
+
|
|
245
|
+
**这样设计的好处**:
|
|
246
|
+
1. 避免与外部工程的状态管理(Vuex、Redux、MobX 等)冲突
|
|
247
|
+
2. 组件库内部可以自主管理状态
|
|
248
|
+
3. 外部工程只需关注传入配置,无需关心组件库内部状态
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## 本地开发测试
|
|
253
|
+
|
|
254
|
+
### 使用 file: 协议引用本地包
|
|
255
|
+
|
|
256
|
+
在测试项目的 `package.json` 中:
|
|
257
|
+
|
|
258
|
+
```json
|
|
259
|
+
{
|
|
260
|
+
"dependencies": {
|
|
261
|
+
"py-test-component": "file:../py-component"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
然后执行:
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
npm install
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### 测试项目结构
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
my-vue2-components/
|
|
276
|
+
├── py-component/ # 组件库源码
|
|
277
|
+
├── vue-test/ # Vue 测试工程
|
|
278
|
+
└── react-test/ # React 测试工程
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 常见问题
|
|
284
|
+
|
|
285
|
+
### Q: React 项目中组件加载很慢?
|
|
286
|
+
|
|
287
|
+
A: 首次加载时需要动态下载和解析 `py-component.esm.js`(约 1.1MB)。建议:
|
|
288
|
+
- 在生产环境使用 CDN 加速
|
|
289
|
+
- 或预加载组件库:`initStore()` 提前调用
|
|
290
|
+
|
|
291
|
+
### Q: 样式不生效?
|
|
292
|
+
|
|
293
|
+
A:
|
|
294
|
+
- **Vue**: 确保引入了 `element-ui/lib/theme-chalk/index.css`
|
|
295
|
+
- **React**: CSS 会自动注入,无需手动引入
|
|
296
|
+
|
|
297
|
+
### Q: propData 可以传什么类型?
|
|
298
|
+
|
|
299
|
+
A: 不限制类型,可以是:
|
|
300
|
+
- 数组(PyTable 需要)
|
|
301
|
+
- 对象(配置项)
|
|
302
|
+
- 任意 JavaScript 值
|
|
303
|
+
|
|
304
|
+
### Q: 如何监听 store 变化?
|
|
305
|
+
|
|
306
|
+
A: **不支持**。store 完全内部化,外部无法监听。如需与外部通信,建议通过组件事件或回调函数。
|
|
307
|
+
|
|
308
|
+
### Q: 如何读取 store 中的配置?
|
|
309
|
+
|
|
310
|
+
A: **不支持**。store 仅供组件库内部使用,外部只能通过 `initStore` 设置,无法读取。
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
## 完整示例
|
|
315
|
+
|
|
316
|
+
### Vue 完整示例
|
|
317
|
+
|
|
318
|
+
```vue
|
|
319
|
+
<!-- App.vue -->
|
|
320
|
+
<template>
|
|
321
|
+
<div id="app">
|
|
322
|
+
<h1>Vue 使用 PyComponent</h1>
|
|
323
|
+
<PyTable :propData="users" />
|
|
324
|
+
<PyWeather />
|
|
325
|
+
</div>
|
|
326
|
+
</template>
|
|
327
|
+
|
|
328
|
+
<script>
|
|
329
|
+
import { PyTable, PyWeather } from 'py-test-component/vue';
|
|
330
|
+
|
|
331
|
+
export default {
|
|
332
|
+
components: { PyTable, PyWeather },
|
|
333
|
+
data() {
|
|
334
|
+
return {
|
|
335
|
+
users: [
|
|
336
|
+
{ date: '2024-01', name: '张三', address: '北京' },
|
|
337
|
+
{ date: '2024-02', name: '李四', address: '上海' }
|
|
338
|
+
]
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
</script>
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### React 完整示例
|
|
346
|
+
|
|
347
|
+
```jsx
|
|
348
|
+
// App.jsx
|
|
349
|
+
import React, { useState } from 'react';
|
|
350
|
+
import { PyTable, PyWeather } from 'py-test-component/react';
|
|
351
|
+
|
|
352
|
+
export default function App() {
|
|
353
|
+
const [data] = useState([
|
|
354
|
+
{ date: '2024-01', name: '张三', address: '北京' },
|
|
355
|
+
{ date: '2024-02', name: '李四', address: '上海' }
|
|
356
|
+
]);
|
|
357
|
+
|
|
358
|
+
return (
|
|
359
|
+
<div>
|
|
360
|
+
<h1>React 使用 PyComponent</h1>
|
|
361
|
+
<PyTable propData={data} loading={<p>加载中...</p>} />
|
|
362
|
+
<PyWeather loading={<p>加载中...</p>} />
|
|
363
|
+
</div>
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
```
|