国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue使用json-server進行后端數據模擬功能

2019-11-19 14:01:23
字體:
來源:轉載
供稿:網友

正開發過程中 前后端分離或者不分離 ,接口多半是之后與頁面的開發 ,所以建立rest的APL的接口 給前端提供虛擬的數據是非常必要的 所以這里我使用了json-server作為工具,支持CORS和JSONP跨域請求,支持GET, POST, PUT, PATCH 和 DELETE 方法,更提供了一系列的查詢方法,如limit,order等,接下來我把我自己的上使用心寫成文檔

安裝

首先必須有node環境(都用到json-server一定會有node環境吧)然后全局安裝json-server

npm install json-server -g

安裝完成后檢查是否全局安裝成功

G:/demo/JavaScript/Vue/Vue one/project/my-project/Vue_two/my-project>json-server -hindex.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.jsonhttps://github.com/typicode/json-server

在項目根目錄創建一個db.json的目錄,然后寫入信息

{ "api": [  {   "text": "數據統計",   "link": "#",   "hot": true  },  {   "text": "數據檢測",   "link": "#",   "hot": true  },  {   "text": "流量分析",   "link": "#",   "hot": true  },  {   "text": "廣告發布",   "link": "#",   "hot": false  } ]}

在package.json里面的scripts里面加一行命令

  "json": "json-server db.json --port 3003"

在項目目錄執行命令

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

恭喜啟動成功!

這時候進入網頁進行訪問

這時候就可以進行訪問了

http://localhost:3003/api

可以看到之前寫的json串

到此json-server啟動完畢

調用的代碼是這樣的

this.$http.get('http://localhost:3003/api')   .then((data) => {    console.log(data.body)   }, () => {    console.log('這里是用了vue-source,后端沒有接口')   })

頁面初始化就可以看到數據 完成

總結

以上所述是小編給大家介紹的Vue使用json-server進行后端數據模擬功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邵阳市| 东乡| 赣州市| 宿州市| 上林县| 韶山市| 浦江县| 财经| 习水县| 襄城县| 阿图什市| 都安| 浦县| 化德县| 武宣县| 天峻县| 右玉县| 廊坊市| 上饶市| 广丰县| 耿马| 丽水市| 阿克苏市| 莒南县| 双鸭山市| 平安县| 工布江达县| 霍州市| 南雄市| 额敏县| 沈阳市| 枣强县| 宁晋县| 浦东新区| 崇阳县| 石城县| 大荔县| 宜丰县| 自治县| 云霄县| 增城市|