首先创建一个本地json文件,放在项目中如下
{ "runRedLight":{ "CurrentPage": 1, "TotalPages": 0, "TotalItems": 0, "ItemsPerPage": 100, "Items":[ {"DEVICEID":"121212","DEVICETYPE":"1","DEVICETIME":"2016-10-10","CREATEDTIME": "2016-10-11"}, {"DEVICEID":"124412","DEVICETYPE":"1","DEVICETIME":"2016-10-10","CREATEDTIME": "2016-10-11"}, {"DEVICEID":"121255","DEVICETYPE":"1","DEVICETIME":"2016-10-10","CREATEDTIME": "2016-10-11"}, {"DEVICEID":"121266","DEVICETYPE":"1","DEVICETIME":"2016-10-10","CREATEDTIME": "2016-10-11"} ] }, "redLightRoad": [ {"CREATEDTIME":"2017-03-03 09:41:44","DEVICEID":"2","ID":"45afdc60f33443d28fe0171d0df40d14","NAME":"1"}, {"CREATEDTIME":"2017-03-03 09:41:52","DEVICEID":"5","ID":"74cf7106f1fa406f9c32ad351e7dfc76","NAME":"2"} ] }
然后在dev-server.js中配置
1.数据读取
1 var appData = require('../data.json'); 2 var runRedLight = appData.runRedLight; 3 var redLightRoad = appData.redLightRoad;
2.定义路由
1 var apiRoute = express.Router(); 2 3 apiRoute.get('/runRedLight',function(req, res){ 4 res.json({ 5 errno:0,//错误码 6 data: runRedLight//具体数据 7 }) 8 }) 9 10 apiRoute.get('/redLightRoad',function(req, res){ 11 res.json({ 12 errno:0, 13 data: runRedLight 14 }) 15 })
3.注册定义的api
1 app.use('/api',apiRoute);
4.在页面使用url
1 this.$http.get("/api/runRedLight").then((response) => { 2 response = response.body; 3 console.log(response.data);//需要这样获取到数组 4 });
自己做项目遇到这个问题,看到别人的这个方法,写下来记录一下。