方法一:
将 mode: 'history' 中的history 替换为 'hash'
const router = new VueRouter({ mode: 'history', routes: [...] })
方法二:
了解 HTML5 History 模式 :https://router.vuejs.org/zh/guide/essentials/history-mode.html
如果不想要很丑的 hash,就要用路由的 history 模式,通过修改 history后端配置 来解决问题
后端配置例子
#Apache
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule>
除了 mod_rewrite
,你也可以使用 FallbackResource
。
#nginx
location / {
try_files $uri $uri/ /index.html;
}
#原生 Node.js
const http = require('http') const fs = require('fs') const httpPort = 80 http.createServer((req, res) => { fs.readFile('index.htm', 'utf-8', (err, content) => { if (err) { console.log('We cannot open "index.htm" file.') } res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }) res.end(content) }) }).listen(httpPort, () => { console.log('Server listening on: http://localhost:%s', httpPort) })