Vue移动端框架Mint UI教程-调用模拟json数据(五)

1:安装
npm install vue-resource前端

 
5640239-9ae54979c8e2a605.png
 
 
5640239-dcf32513703a4934.png
 

2:打开main.js
注册vue

import VueResource from 'vue-resource'
Vue.use(VueResource)
 
5640239-51a314d6f4bec268.png
 

3:在项目里面建立一个json文件webpack

 
5640239-b805a86f48192dd0.png
 

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)
 
5640239-6f6deff78e24e096.png
 

找到 devServer: {},写入如下代码npm

before(app) { app.get('/api/seller', (req, res) => {
        res.json({
          errno: 0,
          data: seller
        })//接口返回json数据,上面配置的数据seller就赋值给data请求后调用
      }),
 
5640239-cc0f80eb9f4eef30.png
 

6:OK,这个时候,能够在浏览器输入咱们的服务接口
http://localhost:8080/api/seller
是能够看到json文件的数据格式的json

 
5640239-0dc2263f27285a09.png
 

 

7:如今要在控制台查看,在当前要查看的页面写出代码api

created () { this.$http.get('http://localhost:8080/api/seller').then((response) => {
         console.log(response)
       })
     }
 
5640239-6290f8e368f0fbc4.png
 

8:npm run dev 运行项目浏览器


 
5640239-57561b445b4d723f.png
 

9:在浏览器里面输入http://localhost:8080
打开项目
注意8080端口要和my.vue里面打印的端口保持一致
调出控制台,能够看见,接口数据已经显示在控制台了app

 
5640239-1b05ac8093038a61.png
 

 

10:接口数据怎么显示在界面