1. 程式人生 > >vue 調用本地json配置

vue 調用本地json配置

參數 const running config pat webp geo 端口 efault

在 webpack.dev.conf.js文件中
/*----------------jsonServer---------*/
/*引入json-server*/
const jsonServer = require(‘json-server‘)
/*搭建一個server*/
const apiServer = jsonServer.create()
/*將db.json關聯到server*/
const apiRouter = jsonServer.router(‘db.json‘)
const middlewares = jsonServer.defaults()
apiServer.use(middlewares)
apiServer.use(apiRouter)
/*監聽端口*/ apiServer.listen(3000, () => { console.log(‘JSON Server is running‘) })

在config文件夾的index.js 配置跨域

/*代理配置表,在這裏可以配置特定的請求代理到對應的API接口*/
    /* 下面的例子將代理請求 /api/getNewsList  到 http://localhost:3000/getNewsList*/
    proxyTable: {
      ‘/api‘: {
        changeOrigin: true, // 如果接口跨域,需要進行這個參數配置
        target: ‘http://localhost:3000‘, //
接口的域名 pathRewrite: { ‘^/api‘: ‘‘ //後面可以使重寫的新路徑,一般不做更改 } } },

vue 調用本地json配置