There is no dev-server.js file in the build folder of vue-cli, how to configure local data

There is no dev-server.js file in the build folder of vue-cli, how to configure local data

Because the latest version of vue-cli has abandoned dev-server.js, just configure it in webpack.dev.conf.js

The configuration of the new version of webpack.dev.conf.js is as follows:
write picture description here
the above picture is the initial draft, just write the code that introduces the local data,
as shown below:
write picture description here

Need to install express dependencies
npm install express --save-devNo need to install 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
        })
      })
    },

Then npm run dev, be sure to restart http://localhost:8080/api/appData to access
http://localhost:8080
write picture description here

The relevant files are as follows: goods.json in
write picture description here
the local src directory

{
  "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.view

<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>

Reference article:
There is no dev-server.js file in the build folder of vue-cli, how to configure mock data

VUE development requests the configuration of local data, the old version dev-server.js, the new version webpack.dev.conf.js

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324611158&siteId=291194637