Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端模拟api数据的两种方式 #25

Open
yanyue404 opened this issue May 21, 2018 · 0 comments
Open

前端模拟api数据的两种方式 #25

yanyue404 opened this issue May 21, 2018 · 0 comments

Comments

@yanyue404
Copy link
Owner

yanyue404 commented May 21, 2018

json-server

1.全局安装

$ npm install -g json-server

2.创建db.json文件

{
  "records": [
    {
      "id": "1",
      "date": "2018-05-17",
      "title": "title 1",
      "amount": 88
    },
    {
      "id": "2",
      "date": "2018-05-18",
      "title": "title 2",
      "amount": 23
    },
    {
      "id": "3",
      "date": "2018-05-19",
      "title": "title 3",
      "amount": 3
    },
    {
      "id": "4",
      "date": "2018-05-20",
      "title": "title 4",
      "amount": 8
    },
    {
      "id": "5",
      "date": "2018-05-21",
      "title": "title 5",
      "amount": 88
    }
  ]
}

3.开启服务并在指定端口更新

db.json目录打开

json-server --watch db.json --port 3006

Snipaste_2019-10-16_12-52-05.png

4.get 获取数据

Snipaste_2019-10-16_12-53-24.png

根据id获取单个详情数据

Snipaste_2019-10-16_12-57-48.png

或者 http://localhost:3000/records?id=1

过滤功能:

Snipaste_2019-10-16_12-59-18.png

使用 .更可以访问深层属性,如:GET /posts?id=1&id=2 GET /comments?author.name=typicode

分页展示功能:

Snipaste_2019-10-16_13-23-23.png

5.put 更新数据

准备更新
Snipaste_2019-10-16_13-14-28.png

发送请求,send
Snipaste_2019-10-16_13-15-37.png

注意:这里是全量更新替换

6.post 添加数据

准备添加
Snipaste_2019-10-16_13-03-45.png

发送请求,send

Snipaste_2019-10-16_13-07-11.png

7.delete 删除数据

准备删除

Snipaste_2019-10-16_13-10-22.png

发送delete请求,send

Snipaste_2019-10-16_13-11-47.png

mockapi

使用方法与上 json-server相似,这里不再赘述...

useful links

参考链接
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant