Vue使用json-server進行后端數(shù)據(jù)模擬功能
正開發(fā)過程中 前后端分離或者不分離 ,接口多半是之后與頁面的開發(fā) ,所以建立rest的APL的接口 給前端提供虛擬的數(shù)據(jù)是非常必要的 所以這里我使用了json-server作為工具,支持CORS和JSONP跨域請求,支持GET, POST, PUT, PATCH 和 DELETE 方法,更提供了一系列的查詢方法,如limit,order等,接下來我把我自己的上使用心寫成文檔
安裝
首先必須有node環(huán)境(都用到json-server一定會有node環(huán)境吧)然后全局安裝json-server
npm install json-server -g
安裝完成后檢查是否全局安裝成功
G:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project>json-server -h
index.js [options] <source>
Options:
--config, -c Path to config file [default: "json-server.json"]
--port, -p Set port [default: 3000]
--host, -H Set host [default: "0.0.0.0"]
--watch, -w Watch file(s) [boolean]
--routes, -r Path to routes file
--middlewares, -m Paths to middleware files [array]
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [boolean]
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
--snapshots, -S Set snapshots directory [default: "."]
--delay, -d Add delay to responses (ms)
--id, -i Set database id property (e.g. _id) [default: "id"]
--foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
[default: "Id"]
--quiet, -q Suppress log messages from output [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
Examples:
index.js db.json
index.js file.js
index.js http://example.com/db.json
https://github.com/typicode/json-server
在項目根目錄創(chuàng)建一個db.json的目錄,然后寫入信息
{
"api": [
{
"text": "數(shù)據(jù)統(tǒng)計",
"link": "#",
"hot": true
},
{
"text": "數(shù)據(jù)檢測",
"link": "#",
"hot": true
},
{
"text": "流量分析",
"link": "#",
"hot": true
},
{
"text": "廣告發(fā)布",
"link": "#",
"hot": false
}
]
}
在package.json里面的scripts里面加一行命令
"json": "json-server db.json --port 3003"
在項目目錄執(zhí)行命令
npm run json
在bash里面會看到這樣的界面
> vue@1.0.0 json g:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project
> json-server db.json --port 3003
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:3003/api
Home
http://localhost:3003
Type s + enter at any time to create a snapshot of the database
恭喜啟動成功!
這時候進入網(wǎng)頁進行訪問
這時候就可以進行訪問了
可以看到之前寫的json串
到此json-server啟動完畢
調(diào)用的代碼是這樣的
this.$http.get('http://localhost:3003/api')
.then((data) => {
console.log(data.body)
}, () => {
console.log('這里是用了vue-source,后端沒有接口')
})
頁面初始化就可以看到數(shù)據(jù) 完成
總結(jié)
以上所述是小編給大家介紹的Vue使用json-server進行后端數(shù)據(jù)模擬功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實例
- vue.js前后端數(shù)據(jù)交互之提交數(shù)據(jù)操作詳解
- vue+vuecli+webpack中使用mockjs模擬后端數(shù)據(jù)的示例
- vue2.0 axios前后端數(shù)據(jù)處理實例代碼
- 詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
- 詳解vue-Resource(與后端數(shù)據(jù)交互)
- vue+element開發(fā)一個谷歌插件的全過程
- 使用vue-element-admin框架從后端動態(tài)獲取菜單功能的實現(xiàn)
- vue+elementUI組件遞歸實現(xiàn)可折疊動態(tài)渲染多級側(cè)邊欄導航
- Vue Element前端應用開發(fā)之獲取后端數(shù)據(jù)
相關(guān)文章
在vue中created、mounted等方法使用小結(jié)
這篇文章主要介紹了在vue中created、mounted等方法使用小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue如何實現(xiàn)左右滑動tab(vue-touch)
這篇文章主要介紹了vue如何實現(xiàn)左右滑動tab(vue-touch),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

