标题可能描述不准确, 大概就是这么个需求:
用 Vue-cli 搭建一个多入口, 多页面的站点, 也就是通过html-webpack-plugin插件会生成多个 .html 文件, 在默认下, 是只有 index.html 这个入口可以用 history 模式, 如: http://pilation.plugin('html-webpack-plugin-after-emit', function(data, cb) { hotMiddleware.publish({ action: 'reload' }) cb() }) })}
2. 修改 build/dev-server.js 文件
主要修改文件中var app = express()到module.exports = app.listen(port, function (err) {之间的代码
var app = express()var indexHTMLvar adminHTML// 引用前面创建的文件, 并将两个保存内容的函数传过去, 这里保存内容的变量写成对象或者数组也可以, 还可以少点代码require('../config/setup-dev-server')(app, { indexUpdated: index => { indexHTML = index }, adminUpdated: index => { adminHTML = index },})// 加载反向代理Object.keys(proxyTable).forEach(function(context) { var options = proxyTable[context] if (typeof options === 'string') { options = { target: options } } app.use(proxyMiddleware(context, options))})// 设置静态文件夹路由var staticPath = path.posix.join(config.assetsPublicPath, config.assetsSubDirectory)app.use(staticPath, express.static('./static'))// 入口1路由app.get(['/', '/category/:id'], (req, res) => { res.send(indexHTML)})// 入口2路由app.get(['/backend', '/backend/*'], (req, res) => { res.send(adminHTML)})// 404 页面app.get('*', (req, res) => { res.send('HTTP STATUS: 404')})app.use(function(req, res, next) { var err = new Error('Not Found') err.status = 404 next(err)})app.use(function(err, req, res) { res.status(err.status || 500) res.send(err.message)})module.exports = app.listen(port, function(err) {3. npm run dev 开始愉快的写代码吧
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。