欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue+webpack模拟后台数据的示例代码

程序员文章站 2022-03-21 20:36:02
一、在webpack-dev-conf.js文件中: 1、在const portfinder = require(‘portfinder')后添加如下内容...

一、在webpack-dev-conf.js文件中:

1、在const portfinder = require(‘portfinder')后添加如下内容

const express = require('express')
const app = express() //请求server
var appdata = require('../mock/goods.json') //加载本地数据文件
var apiroutes = express.router()
app.use(apiroutes) //通过路由请求数据

2、找到devserver,在里面加上before()方法

devserver: {
 clientloglevel: 'warning',
 historyapifallback: {
  rewrites: [
   { from: /.*/, to: path.posix.join(config.dev.assetspublicpath, 'index.html') },
  ],
 },
 hot: true,
 contentbase: false, // since we use copywebpackplugin.
 compress: true,
 host: host || config.dev.host,
 port: port || config.dev.port,
 open: config.dev.autoopenbrowser,
 overlay: config.dev.erroroverlay ? { warnings: false, errors: true } : false,
 publicpath: config.dev.assetspublicpath,
 proxy: config.dev.proxytable,
 quiet: true, // necessary for friendlyerrorsplugin
 watchoptions: {
  poll: config.dev.poll,
 },
 before(app) {
  app.get('/goods', (req, res) => {
   res.json(appdata)
  })
 }
},

二、在goods.json文件:

{
 "status": "0",
 "msg": "",
 "result": [
  {
   "productid": "100001",
   "productname": "小米6",
   "productprice": "2499",
   "productimg": "1.jpg"
  },{
   "productid": "100002",
   "productname": "音箱",
   "productprice": "999",
   "productimg": "2.jpg"
  },{
   "productid": "100003",
   "productname": "电脑",
   "productprice": "199",
   "productimg": "3.jpg"
  }
 ]
}

三、在goodslist.vue文件中:

vue+webpack模拟后台数据的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。