


How to transform Vue-cli into a history mode that supports multiple pages
This article mainly introduces in detail how to transform Vue-cli into a history mode that supports multiple pages. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
The title description may not be accurate, but this is probably the requirement:
Use Vue-cli to build a multi-entry, multi-page site, that is, through the html-webpack-plugin plug-in Multiple .html files will be generated. By default, only the index.html entry can use history mode, such as: http://www.xxx.com/xxx/xxx, while other entries can only use hash mode. For example: http://www.xxx.com/admin.html#/xxx/xxx, because webpack-dev-middleware will point all routes to the index.html file, if it is online, history mode is required. This will cause some trouble.
It’s so stupid. Just after I finished writing the article, I discovered that the connect-history-api-fallback plug-in does this...
Method update As follows:
Modify the build/dev-server.js file
app.use(require('connect-history-api-fallback')())
to
var history = require('connect-history-api-fallback') app.use(history({ rewrites: [ { from: 'index', to: '/index.html'}, // 默认入口 { from: /\/backend/, to: '/backend.html'}, // 其他入口 { from: /^\/backend\/.*$/, to: '/backend.html'}, ] }))
Please refer to the specific rules: https://github.com/bripkens/connect-history-api-fallback
-------------- Please ignore the following code--- -----------
Let's make some changes so that all entries support history mode:
1. First, we create a setup in the build directory -dev-server.js file, the code inside is as follows:
const path = require('path') const webpack = require('webpack') const clientConfig = require('./webpack.dev.conf') // 引入开发环境下的 webpack 配置文件 module.exports = function setupDevServer(app, opts) { const clientCompiler = webpack(clientConfig) // 加载 webpack-dev-middleware 插件 const devMiddleware = require('webpack-dev-middleware')(clientCompiler, { publicPath: clientConfig.output.publicPath, stats: { colors: true, chunks: false } }) app.use(devMiddleware) // 关键代码开始 // 因为开发环境下, 所有的文件都在内存里, 包括由 html-webpack-plugin 生成的 .html 文件, 所以我们需要用 webpack-dev-middleware 提供的 api 从内存里读取 clientCompiler.plugin('done', () => { const fs = devMiddleware.fileSystem // 访问内存 const filePath = path.join(clientConfig.output.path, 'index.html') // 读取的文件, 文件名和 html-webpack-plugin 生成的文件名要求一致 if (fs.existsSync(filePath)) { // 判断下文件是否存在 const index = fs.readFileSync(filePath, 'utf-8') // 从内存里取出 opts.indexUpdated(index) // 将取出的文件通过 indexUpdated 函数返回, 这个函数怎么来的, 后面会说明 } const adminPath = path.join(clientConfig.output.path, 'backend.html') // 同上, 这是第二个入口生成的 .html 文件, 如果还有其他入口, 这个多复制几份 if (fs.existsSync(adminPath)) { const admin = fs.readFileSync(adminPath, 'utf-8') opts.adminUpdated(admin) } }) // 加载热重载模块 app.use(require('webpack-hot-middleware')(clientCompiler)) var hotMiddleware = require('webpack-hot-middleware')(clientCompiler) // 当修改 html-webpack-plugin 模版时, 自动刷新整个页面 clientCompiler.plugin('compilation', function(compilation) { compilation.plugin('html-webpack-plugin-after-emit', function(data, cb) { hotMiddleware.publish({ action: 'reload' }) cb() }) }) }
2. Modify the build/dev-server.js file
Mainly modify the var in the file The code between app = express() to module.exports = app.listen(port, function (err) {
var app = express() var indexHTML var 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 and start writing happily Code Bar
Related recommendations:
Detailed explanation of History mode in HTML5
The above is the detailed content of How to transform Vue-cli into a history mode that supports multiple pages. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When using Windows Shello, a supported camera cannot be found. The common reasons are that the camera used does not support face recognition and the camera driver is not installed correctly. So let's take a look at how to set it up. Windowshello cannot find a supported camera tutorial: Reason 1: The camera driver is not installed correctly 1. Generally speaking, the Win10 system can automatically install drivers for most cameras, as follows, there will be a notification after plugging in the camera; 2. At this time, we open the device Check the manager to see if the camera driver is installed. If not, you need to do it manually. WIN+X, then select Device Manager; 3. In the Device Manager window, expand the camera option, and the camera driver model will be displayed.

Pros and cons of open source software: Understanding the pros and cons of open source projects requires specific code examples In today’s digital age, open source software is getting more and more attention and respect. As a software development model based on the spirit of cooperation and sharing, open source software is widely used in different fields. However, despite the many advantages of open source software, there are also some challenges and limitations. This article will delve into the pros and cons of open source software and demonstrate the pros and cons of open source projects through specific code examples. 1. Advantages of open source software 1.1 Openness and transparency Open source software

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

ASUS tufz790plus supports memory frequency. ASUS TUFZ790-PLUS motherboard is a high-performance motherboard that supports dual-channel DDR4 memory and supports up to 64GB of memory. Its memory frequency is very powerful, up to 4800MHz. Specific supported memory frequencies include 2133MHz, 2400MHz, 2666MHz, 2800MHz, 3000MHz, 3200MHz, 3600MHz, 3733MHz, 3866MHz, 4000MHz, 4133MHz, 4266MHz, 4400MHz, 4533MHz, 4600MHz, 4733MHz and 4800MHz. Whether it is daily use or high performance needs

How to use Flask-Babel to achieve multi-language support Introduction: With the continuous development of the Internet, multi-language support has become a necessary feature for most websites and applications. Flask-Babel is a convenient and easy-to-use Flask extension that provides multi-language support based on the Babel library. This article will introduce how to use Flask-Babel to achieve multi-language support, and attach code examples. 1. Install Flask-Babel. Before starting, we need to install Flask-Bab first.

Some users use the XP system and want to upgrade their graphics cards to gtx960, but are not sure whether gtx960 supports the xp system. In fact, gtx960 supports xp system. We only need to download the driver suitable for xp system from the official website, and then we can use gtx960. Let’s take a look at the specific steps below. Does gtx960 support XP system: GTX960 is compatible with XP system. Just download and install the driver and you're good to go. First, we need to open the NVIDIA official website and navigate to the home page. We then need to find a label or button above the page, it will probably be labeled "Drivers". Once we find this option we need to click on

How to use the Hyperf framework for international support With the rapid development of globalization, many applications need to have multi-language support functions to meet the needs of users in different countries and regions. As a lightweight, high-performance framework, the Hyperf framework provides international support functions and can help developers quickly develop multi-language applications. This article will introduce how to use internationalization functions in the Hyperf framework and provide corresponding code examples. 1. Configure multi-language support. First, you need to configure the Hyperf configuration file.

As we all know, to install the win11 system, you need to ensure that the computer supports TPM2.0 and turns on secure boot. If your computer fails to install win11, it may be because secure boot is not turned on. The following are tutorials for enabling secure boot on some brands of computers. I hope it will be helpful to you. What should I do if I get a message that secure boot must be supported when upgrading to win11? 1. ASUS motherboard 1. First, we switch to Chinese, and then press F7 on the keyboard to open the advanced settings according to the prompts. 3. Then select Key Management. 2. Lenovo computers 1. For Lenovo computer models before 2020, you need to use F2 to enter the bios settings, and then select security above. 2. In the security tab, drop secureboot and change it to E
