欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解vue 模拟后台数据(加载本地json文件)调试

程序员文章站 2022-09-08 21:31:17
本文介绍了vue 模拟后台数据(加载本地json文件)调试,分享给大家,也给自己留个笔记 首先创建一个本地json文件,放在项目中如下 { "runredl...

本文介绍了vue 模拟后台数据(加载本地json文件)调试,分享给大家,也给自己留个笔记

首先创建一个本地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.数据读取

var appdata = require('../data.json');
var runredlight = appdata.runredlight;
var redlightroad = appdata.redlightroad;

2.定义路由

var apiroute = express.router();
apiroute.get('/runredlight',function(req, res){
 res.json({
  errno:0,//错误码
  data: runredlight//具体数据
 })
})

apiroute.get('/redlightroad',function(req, res){
 res.json({
  errno:0,
  data: runredlight
 })
})

3.注册定义的api

app.use('/api',apiroute);

4.在页面使用url

 this.$http.get("/api/runredlight").then((response) => {
     response = response.body;
     console.log(response.data);//需要这样获取到数组
   });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。