1:安装
npm install vue-resource前端
2:打开main.js
注册vue
import VueResource from 'vue-resource' Vue.use(VueResource)
3:在项目里面建立一个json文件webpack
4:json文件的内容web
{ "seller": { "name": "我是王小婷", "description": "前端开发工程师", "supports": [ { "type": 0, "description": "日更博客打卡" }, { "type": 1, "description": "90后前端妹子" } ] } }
5:打开build文件底下的webpack.dev.conf.js
写入代码express
const express = require('express') const app = express() var appData = require('../data.json') //加载本地数据文件 var seller = appData.seller //获取对应的本地数据 var goods = appData.goods var ratings = appData.ratings var apiRoutes = express.Router() app.use('/api', apiRoutes)
找到 devServer: {},写入如下代码npm
before(app) { app.get('/api/seller', (req, res) => { res.json({ errno: 0, data: seller })//接口返回json数据,上面配置的数据seller就赋值给data请求后调用 }),
6:OK,这个时候,能够在浏览器输入咱们的服务接口
http://localhost:8080/api/seller
是能够看到json文件的数据格式的json
7:如今要在控制台查看,在当前要查看的页面写出代码api
created () { this.$http.get('http://localhost:8080/api/seller').then((response) => { console.log(response) }) }
8:npm run dev 运行项目浏览器
9:在浏览器里面输入http://localhost:8080
打开项目
注意8080端口要和my.vue里面打印的端口保持一致
调出控制台,能够看见,接口数据已经显示在控制台了app
10:接口数据怎么显示在界面