最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

json-server实现后端数据模拟

来源:动视网 责编:小采 时间:2020-11-27 19:51:17
文档

json-server实现后端数据模拟

json-server实现后端数据模拟:这次给大家带来json-server实现后端数据模拟,json-server实现后端数据模拟的注意事项有哪些,下面就是实战案例,一起来看一下。正开发过程中 前后端分离或者不分离 ,接口多半是之后与页面的开发 ,所以建立rest的APL的接口 给前端提供虚拟的数据是非常必要的
推荐度:
导读json-server实现后端数据模拟:这次给大家带来json-server实现后端数据模拟,json-server实现后端数据模拟的注意事项有哪些,下面就是实战案例,一起来看一下。正开发过程中 前后端分离或者不分离 ,接口多半是之后与页面的开发 ,所以建立rest的APL的接口 给前端提供虚拟的数据是非常必要的


这次给大家带来json-server实现后端数据模拟,json-server实现后端数据模拟的注意事项有哪些,下面就是实战案例,一起来看一下。

正开发过程中 前后端分离或者不分离 ,接口多半是之后与页面的开发 ,所以建立rest的APL的接口 给前端提供虚拟的数据是非常必要的 所以这里我使用了json-server作为工具,支持CORS和JSONP跨域请求,支持GET, POST, PUT, PATCH 和 DELETE 方法,更提供了一系列的查询方法,如limit,order等,接下来我把我自己的上使用心写成文档

安装

首先必须有node环境(都用到json-server一定会有node环境吧)然后全局安装json-server

npm install json-server -g

安装完成后检查是否全局安装成功

G:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project>json-server -h
index.js [options] <source>
Options:
 --config, -c Path to config file [default: "json-server.json"]
 --port, -p Set port [default: 3000]
 --host, -H Set host [default: "0.0.0.0"]
 --watch, -w Watch file(s) [boolean]
 --routes, -r Path to routes file
 --middlewares, -m Paths to middleware files [array]
 --static, -s Set static files directory
 --read-only, --ro Allow only GET requests [boolean]
 --no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
 --no-gzip, --ng Disable GZIP Content-Encoding [boolean]
 --snapshots, -S Set snapshots directory [default: "."]
 --delay, -d Add delay to responses (ms)
 --id, -i Set database id property (e.g. _id) [default: "id"]
 --foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
 [default: "Id"]
 --quiet, -q Suppress log messages from output [boolean]
 --help, -h Show help [boolean]
 --version, -v Show version number [boolean]
Examples:
 index.js db.json
 index.js file.js
 index.js http://example.com/db.json
https://github.com/typicode/json-server

在项目根目录创建一个db.json的目录,然后写入信息

{
 "api": [
 {
 "text": "数据统计",
 "link": "#",
 "hot": true
 },
 {
 "text": "数据检测",
 "link": "#",
 "hot": true
 },
 {
 "text": "流量分析",
 "link": "#",
 "hot": true
 },
 {
 "text": "广告发布",
 "link": "#",
 "hot": false
 }
 ]
}

在package.json里面的scripts里面加一行命令

 "json": "json-server db.json --port 3003"

在项目目录执行命令

npm run json

在bash里面会看到这样的界面

> vue@1.0.0 json g:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project
> json-server db.json --port 3003
 \{^_^}/ hi!
 Loading db.json
 Done
 Resources
 http://localhost:3003/api
 Home
 http://localhost:3003
 Type s + enter at any time to create a snapshot of the database

恭喜启动成功!

这时候进入网页进行访问

这时候就可以进行访问了

http://localhost:3003/api

可以看到之前写的json串

到此json-server启动完毕

调用的代码是这样的

this.$http.get('http://localhost:3003/api')
 .then((data) => {
 console.log(data.body)
 }, () => {
 console.log('这里是用了vue-source,后端没有接口')
 })

页面初始化就可以看到数据 完成

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Vue-cropper对图片进行裁剪步骤详解

使用Vue Mixin功能步骤详解

文档

json-server实现后端数据模拟

json-server实现后端数据模拟:这次给大家带来json-server实现后端数据模拟,json-server实现后端数据模拟的注意事项有哪些,下面就是实战案例,一起来看一下。正开发过程中 前后端分离或者不分离 ,接口多半是之后与页面的开发 ,所以建立rest的APL的接口 给前端提供虚拟的数据是非常必要的
推荐度:
标签: 数据 实现 模拟
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top