• mock数据时,http://localhost:8080/#/api/goods 无法访问到数据


    最近学习一个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 插件将返回数据格式化

     

  • 相关阅读:
    树莓派上使用docker部署aria2,minidlna
    在Ubuntu上部署一个基于webrtc的多人视频聊天服务
    解决.net core3.1使用docker部署在Ubuntu上连接sqlserver报error:35的问题
    .Net Core in Docker
    .Net Core in Docker
    SQL Server in Docker
    使用Docker运行SQL Server
    Spring Boot 2.x(四):整合Mybatis的四种方式
    Spring Boot 2.x (一):HelloWorld
    yarn (npm) 切换设置镜像源
  • 原文地址:https://www.cnblogs.com/liubingyjui/p/10801609.html
Copyright © 2020-2023  润新知