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


正開發過程中 前后端分離或者不分離 ,接口多半是之后與頁面的開發 ,所以建立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 -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

在項目根目錄創建一個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,后端沒有接口')
      })

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM