首页 技术 正文
技术 2022年11月7日
0 收藏 332 点赞 256 浏览 3111 个字

json-server搭建模拟的API服务器

  • 运行命令 npm install json-server -D 全局安装 json-server

  • 项目根目录下创建 mock 文件夹

  • mock 文件夹下添加 db.json 文件,内容如下:

{
"students": [
{ "id": 1, "name": "小明", "age": 20},
{ "id": 2, "name": "小红", "age": 21},
{ "id": 3, "name": "小白", "age": 19},
{ "id": 4, "name": "小露", "age": 20},
{ "id": 5, "name": "小彩", "age": 22}
],
"country": [
{"name": "中国"},
{"name": "日本"},
{"name": "荷兰"},
{"name": "美国"}
]
}
  • package.json 添加命令:

  ”mock”: “json-server –port 1995 mock/db.js”,

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server --open --port 80 --hot",
"mock": "json-server --port 1995 mock/db.js",
"build": "webpack"
},
  • 打开终端,运行 npm run mock 运行 json-server 服务器

faker.js 和 lodash.js 创建假数据

  • 运行 npm i faker -D 安装生成假数据的包;

  • 运行 npm i lodash -S 安装lodash,利用 _.times() 方法循环调用 faker 生成数据;

  • mock 目录下的 db.json 改名为 db.js(注意把package.json的mock的db.json改成db.js)

  • 修改 db.js 中的代码如下:

// 导入 lodash
const _ = require('lodash')// 导入 faker 生成假数据
const faker = require('faker')// 设置本地化
faker.lacale = 'zh_CN'// 使用commonJS 规范把生成的数据导出
// 其中, module.exports 必须指向函数,而且函数中,必须 return 一个数据对象*/
module.exports = () => {
// json-server 最终return的数据,必须是一个对象
const data = { list: [] }
// 调用 _.times 数据生成8次数据
data.list = _.times(8, n => {
return {
id: n + 1,
name: faker.random.word(),
ctime: faker.date.recent()
}
})
// 把生成的数据返回出去
return data
}

  Vue.js(15)之 json-server搭建模拟的API服务器

案例

index.js

import Vue from 'vue'import Router from 'vue-router'Vue.use(Router)// 导入并配置  axios
import axios from 'axios'
Vue.prototype.$http = axiosimport app from './app.vue'
import list from './list.vue'
import detail from './detail.vue'const router = new Router({
routes: [
{ path: '/', redirect: '/list'},
{ path: '/list', component: list },
{ path: '/list/detail/:id', component: detail, props: true }
]
})const vm = new Vue({
el: '#app',
render: c => c(app),
router
})

app.vue

<template>
<div>
<h1>APP根组件</h1>
<router-view></router-view>
</div>
</template>

list.vue

<template>
<div>
<h3>列表</h3> <ul>
<li v-for="item in list" :key="item.id" @click="goDetail(item.id)">
{{item.name}}
</li>
</ul>
</div>
</template><script>
export default {
data() {
return {
// 定义列表数据,默认为空数组
list: []
}
},
created() {
this.getMovieList()
},
methods: {
// 获取列表的数据
async getMovieList() {
const { data: res } = await this.$http.get('http://localhost:1995/list')
this.list = res
},
// 点击,通过编程式导航,跳转到详情页面
goDetail(id) {
this.$router.push('/list/detail/' + id)
}
}
}
</script><style lang="less" scoped>
li {
list-style: none;
line-height: 35px;
border: 1px dashed #ccc;
margin: 10px 0;
font-size: 12px;
}
</style>

detail.vue

<template>
<div>
<h3>列表</h3> <ul>
<li v-for="item in list" :key="item.id" @click="goDetail(item.id)">
{{item.name}}
</li>
</ul>
</div>
</template><script>
export default {
data() {
return {
// 定义列表数据,默认为空数组
list: []
}
},
created() {
this.getMovieList()
},
methods: {
// 获取列表的数据
async getMovieList() {
const { data: res } = await this.$http.get('http://localhost:1995/list')
this.list = res
},
// 点击,通过编程式导航,跳转到详情页面
goDetail(id) {
this.$router.push('/list/detail/' + id)
}
}
}
</script><style lang="less" scoped>
li {
list-style: none;
line-height: 35px;
border: 1px dashed #ccc;
margin: 10px 0;
font-size: 12px;
}
</style>

db.js

// 导入 lodash
const _ = require('lodash')// 导入 faker 生成假数据
const faker = require('faker')// 设置本地化
faker.lacale = 'zh_CN'// 使用commonJS 规范把生成的数据导出
// 其中, module.exports 必须指向函数,而且函数中,必须 return 一个数据对象*/
module.exports = () => {
// json-server 最终return的数据,必须是一个对象
const data = { list: [] }
// 调用 _.times 数据生成8次数据
data.list = _.times(8, n => {
return {
id: n + 1,
name: faker.random.word(),
ctime: faker.date.recent()
}
})
// 把生成的数据返回出去
return data
}

Vue.js(15)之 json-server搭建模拟的API服务器

Vue.js(15)之 json-server搭建模拟的API服务器

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896