py-test-component 1.0.11 → 2.0.2
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/README.md +201 -0
- package/USAGE.md +366 -0
- package/dist/py-component.esm.js +1 -1
- package/dist/py-component.js +1 -1
- package/package.json +2 -7
- package/src/react/index.js +27 -119
- package/src/vue/index.js +8 -2
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# py-test-component
|
|
2
|
+
|
|
3
|
+
Vue2 + ElementUI 组件库,支持在 React 项目中使用。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- **跨框架支持**:Vue 2 项目原生使用,React 项目无缝集成
|
|
8
|
+
- **Web Components 技术**:基于 `vue-custom-element` 将 Vue 组件转换为自定义元素
|
|
9
|
+
- **统一 API**:Vue 和 React 项目使用相同的组件接口
|
|
10
|
+
- **自动样式注入**:React 使用时自动加载 ElementUI CSS
|
|
11
|
+
- **全局状态管理**:内置基于 Vue.observable 的 store,支持跨组件状态共享
|
|
12
|
+
|
|
13
|
+
## 组件列表
|
|
14
|
+
|
|
15
|
+
| 组件 | 说明 | 参数 |
|
|
16
|
+
|------|------|------|
|
|
17
|
+
| PyTable | 表格展示组件 | `propData`: 任意类型数据 |
|
|
18
|
+
| PyWeather | 天气查询组件 | `propData`: 配置对象 |
|
|
19
|
+
|
|
20
|
+
## 项目结构
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
py-component/
|
|
24
|
+
├── dist/ # 构建产物
|
|
25
|
+
│ ├── py-component.js # UMD 格式(浏览器直接使用)
|
|
26
|
+
│ └── py-component.esm.js # ESM 格式(构建工具使用)
|
|
27
|
+
├── src/
|
|
28
|
+
│ ├── components/ # Vue 组件源码
|
|
29
|
+
│ │ ├── PyTable.vue
|
|
30
|
+
│ │ └── PyWeather.vue
|
|
31
|
+
│ ├── react/ # React 适配器
|
|
32
|
+
│ │ └── index.js # React 组件包装器
|
|
33
|
+
│ ├── vue/ # Vue 入口
|
|
34
|
+
│ │ └── index.js # 直接导出 Vue 组件
|
|
35
|
+
│ ├── store/ # 全局状态管理
|
|
36
|
+
│ │ └── index.js
|
|
37
|
+
│ ├── utils/ # 工具函数
|
|
38
|
+
│ │ ├── api.js # API 封装
|
|
39
|
+
│ │ └── request.js # fetch 请求封装
|
|
40
|
+
│ └── index.js # 主入口(Web Components 注册)
|
|
41
|
+
├── package.json
|
|
42
|
+
└── webpack.config.js
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 开发教程
|
|
46
|
+
|
|
47
|
+
### 环境准备
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
cd py-component
|
|
51
|
+
npm install
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 开发模式
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm run dev
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
监听文件变化,自动重新构建。
|
|
61
|
+
|
|
62
|
+
### 生产构建
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm run build
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
生成 `dist/py-component.js` 和 `dist/py-component.esm.js`。
|
|
69
|
+
|
|
70
|
+
### 添加新组件
|
|
71
|
+
|
|
72
|
+
1. **创建 Vue 组件** `src/components/PyNewComponent.vue`:
|
|
73
|
+
|
|
74
|
+
```vue
|
|
75
|
+
<template>
|
|
76
|
+
<div class="py-new-component">
|
|
77
|
+
<!-- 组件内容 -->
|
|
78
|
+
</div>
|
|
79
|
+
</template>
|
|
80
|
+
|
|
81
|
+
<script>
|
|
82
|
+
export default {
|
|
83
|
+
name: 'PyNewComponent',
|
|
84
|
+
props: {
|
|
85
|
+
propData: {
|
|
86
|
+
default: null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
</script>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
2. **注册 Web Component**(`src/index.js`):
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import PyNewComponent from './components/PyNewComponent.vue';
|
|
97
|
+
|
|
98
|
+
Vue.customElement('py-new-component', PyNewComponent, {
|
|
99
|
+
shadow: false
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
3. **Vue 入口导出**(`src/vue/index.js`):
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
import PyNewComponent from '../components/PyNewComponent.vue';
|
|
107
|
+
export { PyNewComponent };
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
4. **React 入口导出**(`src/react/index.js`):
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
export const PyNewComponent = wrapVueComponent('py-new-component', 'propData');
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 发布教程
|
|
117
|
+
|
|
118
|
+
### 1. 更新版本号
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
npm version patch # 补丁版本 1.0.0 -> 1.0.1
|
|
122
|
+
npm version minor # 次要版本 1.0.0 -> 1.1.0
|
|
123
|
+
npm version major # 主要版本 1.0.0 -> 2.0.0
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 2. 构建产物
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm run build
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 3. 发布到 npm
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm publish
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 4. 本地测试(模拟发布)
|
|
139
|
+
|
|
140
|
+
在测试项目中使用 `file:` 协议引用:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"dependencies": {
|
|
145
|
+
"py-test-component": "file:../py-component"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## 注意事项
|
|
151
|
+
|
|
152
|
+
### 1. CSS 加载
|
|
153
|
+
|
|
154
|
+
- **Vue 项目**:需自行引入 ElementUI CSS
|
|
155
|
+
```javascript
|
|
156
|
+
import 'element-ui/lib/theme-chalk/index.css';
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
- **React 项目**:CSS 自动注入(通过 CDN),无需手动引入
|
|
160
|
+
|
|
161
|
+
### 2. 组件数据传递
|
|
162
|
+
|
|
163
|
+
所有组件统一使用 `propData` 属性传递数据,不限制数据类型:
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
// 可以是数组
|
|
167
|
+
<PyTable :propData="[{ name: '张三' }]" />
|
|
168
|
+
|
|
169
|
+
// 可以是对象
|
|
170
|
+
<PyWeather :propData="{ city: '北京' }" />
|
|
171
|
+
|
|
172
|
+
// 可以是任意类型
|
|
173
|
+
<AnyComponent :propData="任意值" />
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 3. Store 使用限制
|
|
177
|
+
|
|
178
|
+
- **Vue 项目**:可直接使用响应式 store
|
|
179
|
+
- **React 项目**:只能通过 `initStore` 设置值,无法直接获取(因 Vue.observable 与 React 响应式系统不兼容)
|
|
180
|
+
|
|
181
|
+
### 4. 构建体积
|
|
182
|
+
|
|
183
|
+
当前构建产物包含 Vue 和 ElementUI 完整代码(约 1.1MB),如需优化体积,建议配置 Webpack externals 将依赖作为 peerDependencies 外部化。
|
|
184
|
+
|
|
185
|
+
### 5. 浏览器兼容性
|
|
186
|
+
|
|
187
|
+
- 支持现代浏览器(Chrome、Firefox、Safari、Edge)
|
|
188
|
+
- IE11 需要 polyfill
|
|
189
|
+
|
|
190
|
+
## 目录说明
|
|
191
|
+
|
|
192
|
+
| 目录/文件 | 说明 |
|
|
193
|
+
|-----------|------|
|
|
194
|
+
| `dist/` | 构建产物,发布时包含 |
|
|
195
|
+
| `src/components/` | Vue 组件源码 |
|
|
196
|
+
| `src/react/` | React 适配器代码 |
|
|
197
|
+
| `src/vue/` | Vue 直接入口 |
|
|
198
|
+
| `src/store/` | 全局状态管理 |
|
|
199
|
+
| `src/utils/` | 工具函数(请求、API) |
|
|
200
|
+
| `vue-test/` | Vue 测试工程(同级目录) |
|
|
201
|
+
| `react-test/` | React 测试工程(同级目录) |
|
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
|
+
```
|