1. 程式人生 > >vue-cli的build的資料夾下沒有dev-server.js檔案,如何配置本地資料

vue-cli的build的資料夾下沒有dev-server.js檔案,如何配置本地資料

vue-cli的build的資料夾下沒有dev-server.js檔案,怎麼配置本地資料

因為最新版本的vue-cli已經放棄dev-server.js,只需在webpack.dev.conf.js配置就行

新版webpack.dev.conf.js配置如下:
這裡寫圖片描述
上面一張圖是初始稿,只寫引入本地資料那一條程式碼即可,
如下圖:
這裡寫圖片描述

需要安裝express依賴
npm install express --save-dev 不需要安裝express

const portfinder = require(‘portfinder’)後新增

const express = require
('express') // 不需要 const app = express() // 不需要 var appData = require('../src/goods.json') //載入本地資料檔案 var router = express.Router() // 不需要 app.use('/api', router) // 不需要
//然後找到devServer,在裡面新增
//介面返回json資料,下面配置的資料appData就賦值給data請求後呼叫
    before(app) {
      app.get('/api/appData', (req, res) => {
        res.json({
          error: 0
, data: appData }) })
},

相關檔案詳細如下:
這裡寫圖片描述
本地src目錄下
goods.json

{
  "error": 0,
  "data": {
    "status": "0",
    "result": [
      {
        "productId": "10000",
        "productName": "小米6",
        "productPrice": "2499",
        "productImg": "http://p0scbmmzr.bkt.clouddn.com/img/avatar0.jpg"
}, { "productId": "10001", "productName": "小米6", "productPrice": "2499", "productImg": "http://p0scbmmzr.bkt.clouddn.com/img/avatar1.jpg" }, { "productId": "10002", "productName": "小米6", "productPrice": "2499", "productImg": "http://p0scbmmzr.bkt.clouddn.com/img/avatar2.jpg" }, { "productId": "10003", "productName": "小米6", "productPrice": "2499", "productImg": "http://p0scbmmzr.bkt.clouddn.com/img/avatar3.jpg" }, { "productId": "10004", "productName": "小米6", "productPrice": "2499", "productImg": "http://p0scbmmzr.bkt.clouddn.com/img/avatar4.jpg" } ]
}
}

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'

Vue.prototype.$axios = axios
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

App.vue

<template>
  <div id="app">
    <HelloWorld/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  text-align: center;
}
</style>

HelloWorld.vue

<template>
  <div class="hello">
    <ul>
      <li v-for="(item, key) in data">
        <div class="box">
          <img :src="item.productImg" alt="" class="figure">
          <p>{{item.productId}}</p>
          <p>{{item.productName}}</p>
          <p>{{item.productPrice}}</p>
          <hr/>
        </div>
      </li>
    </ul>

  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      data: []
    }
  },
  mounted () {
    this.$axios.get('/api/appData').then(res => {
      this.data = res.data.data.data.result;
      console.log(res.data.data.data.result);
    })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.figure {
  width: 100px;
  height: 100px;
  border-radius: 50px;
}
</style>