最近学习一个vue-cli的项目,需要与后台进行数据交互,这里使用本地json数据来模仿后台数据交互流程。然而发现build文件夹下没有dev-server.js文件了,因为新版本的vue-webpack-template 中已经去掉了dev-server.js,取而代之的是webpack.dev.conf.js文件,所以可以在webpack.dev.conf.js里配置本地访问。

将build目录下的webpack.dev.conf.js文件改为:

'use strict'
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');
const merge = require('webpack-merge');
const path = require('path');
const baseWebpackConfig = require('./webpack.base.conf');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const portfinder = require('portfinder');
//引入数据,定义数据变量
var appData = require('../data.json');
var seller = appData.seller;
var goods = appData.goods;
var ratings = appData.ratings; const HOST = process.env.HOST;
const PORT = process.env.PORT && Number(process.env.PORT); const devWebpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
},
// cheap-module-eval-source-map is faster for development
devtool: config.dev.devtool, // these devServer options should be customized in /config/index.js
devServer: {
before(app) {
app.get('/api/seller', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: seller //用data是更好一点
});
}),
app.get('/api/goods', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: goods //用data是更好一点
});
}),
app.get('/api/ratings', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: ratings //用data是更好一点
});
});
},
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,
}
},
plugins: [
new webpack.DefinePlugin({
'process.env': require('../config/dev.env')
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
])
]
});
module.exports = new Promise((resolve, reject) => {
portfinder.basePort = process.env.PORT || config.dev.port
portfinder.getPort((err, port) => {
if (err) {
reject(err)
} else {
// publish the new Port, necessary for e2e tests
process.env.PORT = port
// add port to devServer config
devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
compilationSuccessInfo: {
messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
},
onErrors: config.dev.notifyOnErrors
? utils.createNotifierCallback()
: undefined
})) resolve(devWebpackConfig)
}
})
})

 

npm run dev,启动后输入http://localhost:8080/api/seller,如果数据正常显示,则数据能正常返回

PS:Google可以用 jsonview 插件将返回数据格式化

 

mock数据时,http://localhost:8080/#/api/goods 无法访问到数据的更多相关文章

  1. inserted触发器,一张表插入数据时,同时向另外一张表插入数据

    有时候,一个服务器上有多个数据库,需要向其中一个数据库的表中插入数据时, 同时向另外一个数据的表里插入数据. 可以利用触发器和同义词(建立同义词的方法省略), 在一个数据库的表里插入数据时,同时向另外 ...

  2. Node 跨域问题 Access to XMLHttpRequest at 'http://localhost:8080/api/user/login' from origin 'http://localhost:808

    人不可能踏进同一条河流,我可以一天在同一个问题上摔倒两次. 这次是跨域问题,都是泪,教程提供的服务端代码虽然配置了文件,但是依然是没有解决跨域问题,依然报错 Request header field ...

  3. 扩展当easyui datagrid无数据时,显示特定值。如:没有数据

    var myview = $.extend({},$.fn.datagrid.defaults.view,{ onAfterRender:function(target){ $.fn.datagrid ...

  4. Tomcat localhost 8080打不开

    最近发现一个问题,使用tomcat部署项目时,项目可以正常访问,但是localhost 8080打不开,总是出现404 为了找回那只可爱的tom,我先做了个测试 在tomcat的webapps文件夹下 ...

  5. Logstash使用jdbc_input同步Mysql数据时遇到的空时间SQLException问题

    今天在使用Logstash的jdbc_input插件同步Mysql数据时,本来应该能搜索出10条数据,结果在Elasticsearch中只看到了4条,终端中只给出了如下信息 [2017-08-25T1 ...

  6. FreeSql (七)插入数据时忽略列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

  7. FreeSql (八)插入数据时指定列

    插入数据时指定列,和忽略列对应,未被指定的列将被忽略. var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Passwor ...

  8. FreeSql (十二)更新数据时指定列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

  9. FreeSql (十三)更新数据时忽略列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

随机推荐

  1. MAKEFILE_LIST/CURDIR/MAKECMDGOALS/MAKEOVERRIDES/MAKEFLAGS

    http://blog.chinaunix.net/uid-29460203-id-4191975.html https://www.xuebuyuan.com/1148403.html?mobile ...

  2. Python spyder-快捷键-多行注释

    选中多行后: Ctrl + 1: 注释/反注释 Ctrl + 4/5: 块注释/块反注释 Ctrl + L: 跳转到行号 Tab/Shift + Tab: 代码缩进/反缩进

  3. Ubuntu16.04 QT5编译出现cannot find -lGL和collect2:error:ld r

    在ubuntu下使用Qt 编译时候遇上了cannot find -lGL错误,使用命令 是由于系统缺少链接库,在终端执行下面命令就可以解决问题. sudo apt-get install libqt4 ...

  4. js实现汉字转拼音

    汉字转拼音,每个字首字母大写:pinyin.getFullChars(name); 提取首字母并大写:pinyin.getCamelChars(name); /* --- description: P ...

  5. zxEditor

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="X-UA-C ...

  6. c++调用自己编写的静态库(通过eclipse)

    转:https://blog.csdn.net/hao5335156/article/details/80282829 参考:https://blog.csdn.net/u012707739/arti ...

  7. 如何在项目中新建.gitignore文件

    1. 在需要创建 .gitignore 文件的文件夹, 右键选择 Git Bash 进入命令行,进入项目所在目录. 2. 输入 touch .gitignore 在文件夹就生成了一个“.gitigno ...

  8. IDE - IDEA - tab - 方法相关的移动

    1. 概述 标题可能会改 一个 tab 里方法相关的操作 2. 前提 以默认的模式编辑 tab 对我来说, 就关掉 vim 插件 3. 操作 1. 查看文件结构 概述 唤出当前文件的 结构 唤出后可以 ...

  9. JS高级---三种创建对象的方式

    JS高级---三种创建对象的方式 字面量的方式 (实例对象) 调用系统的构造函数 自定义构造函数方式 //创建对象---->实例化一个对象,的同时对属性进行初始化 var per=new Per ...

  10. 【Math】高数-一个有趣的旋转体体积与面积

    Go confidently in the direction of your dreams. Live the life you've imagined. 题目 设曲线 \(y = \tfrac{1 ...