forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request umijs#638 from leftstick/master
docs: documentation for mocking data
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Mock 数据 | ||
|
||
`umi`支持 静态模拟数据 和 动态模拟数据 两种形态的数据模拟操作。 | ||
|
||
`umi` 中约定 `mock` 目录下的 `*.js` 会被用来生成模拟API数据。 | ||
|
||
### Mock 配置 | ||
|
||
Mock 文件配置参考如下pattern: | ||
|
||
``` | ||
[HTTP-Method API-Path]: <Static Data Object> | <Dynamic Data Provider> | ||
``` | ||
|
||
**具体示例** | ||
|
||
假设 `mock` 目录结构如下: | ||
|
||
``` | ||
+ mock/ | ||
- users.js | ||
- cities.js | ||
``` | ||
|
||
### 静态数据模拟 | ||
|
||
然后在 `users.js` 里做如下配置 | ||
|
||
```js | ||
export default { | ||
'get /users': { | ||
result: [ | ||
{ | ||
id: 1, | ||
name: 'zhangsan', | ||
alias: '张三', | ||
email: '[email protected]', | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
`dev`启动后,可以通过`/users`直接在代码请求该API。 | ||
|
||
### 动态数据模拟配置 | ||
|
||
|
||
然后在 `cities.js` 里做如下配置 | ||
|
||
```js | ||
export default { | ||
'get /cities': function (req, res, next) { | ||
setTimeout(() => { | ||
res.json({ | ||
result: [ | ||
{ | ||
id: 1, | ||
name: 'beijing', | ||
alias: '北京' | ||
} | ||
] | ||
}) | ||
}, 1500) | ||
} | ||
} | ||
``` | ||
|
||
`dev`启动后,可以通过`/cities`直接在代码请求该API,`1.5s`后得到了返回数据。 | ||
|
||
|
||
>mock 使开发更轻松 |