이 기사에서는 정적 json 데이터를 얻기 위해 http 요청을 사용하는 vue2.5.2의 예제 코드를 주로 소개합니다. 필요한 친구는 이를 참조할 수 있습니다.
1 Build/webpack.dev.conf.js
// 获取静态json数据
const express = require('express')
const app = express()
const apiServer = express()
const bodyParser = require('body-parser')
apiServer.use(bodyParser.urlencoded({ extended: true }))
apiServer.use(bodyParser.json())
const apiRouter = express.Router()
const fs = require('fs')
apiRouter.route('/:apiName')
.all(function (req, res) {
fs.readFile('./db.json', 'utf8', function (err, data) {
if (err) throw err
var data = JSON.parse(data)
if (data[req.params.apiName]) {
res.json(data[req.params.apiName])
}
else {
res.send('no such api name')
}
})
})
apiServer.use('/api', apiRouter);
apiServer.listen(8081, function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:' + (8081) + '\n')
})
2. 새로운 db.json
{
"getNewsList": [
{
"id": 1,
"title": "新闻条目1新闻条目1新闻条目1新闻条目1",
"url": "http://starcraft.com"
},
{
"id": 2,
"title": "新闻条目2新闻条目2新闻条目2新闻条目2",
"url": "http://warcraft.com"
},
{
"id": 3,
"title": "新闻条3新闻条3新闻条3",
"url": "http://overwatch.com"
},
{
"id": 4,
"title": "新闻条4广告发布",
"url": "http://hearstone.com"
}
],
"login": {
"username": "yudongdong",
"userId": 123123
},
"getPrice": {
"amount": 678
},
"createOrder": {
"orderId": "6djk979"
},
"getOrderList": {
"list": [
{
"orderId": "ddj123",
"product": "数据统计",
"version": "高级版",
"period": "1年",
"buyNum": 2,
"date": "2016-10-10",
"amount": "500元"
},
{
"orderId": "yuj583",
"product": "流量分析",
"version": "户外版",
"period": "3个月",
"buyNum": 1,
"date": "2016-5-2",
"amount": "2200元"
},
{
"orderId": "pmd201",
"product": "广告发布",
"version": "商铺版",
"period": "3年",
"buyNum": 12,
"date": "2016-8-3",
"amount": "7890元"
}
]
}
}
3을 생성합니다. 3. localhost:8081/api/getNewsList
4를 통해 액세스합니다.
위의 저입니다. 모두를 위해 정리했는데, 앞으로 모든 분들께 도움이 되길 바랍니다.
관련 기사:
선택 드롭다운 상자를 사용하여 vue.js에서 바인딩 및 값 메서드를 구현합니다.vue에서 v-for를 사용할 때 빨간색 및 경고 문제를 해결하는 방법(자세한 튜토리얼)
Vuejs에서 검색 일치 기능 메서드를 구현하는 방법(자세한 튜토리얼)
위 내용은 vue2.5.2에서 http 요청을 사용하여 정적 json 데이터를 얻는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!