的VUE路由器默認模式爲散模式 - 它使用URL散列 模擬一個完整的URL,這樣的頁面將不會在URL 發生變化時重新加載。
爲了擺脫散的,我們可以使用路由器的歷史模式, 利用了history.pushState API實現網址導航不 重新加載頁面:
常量路由器=新VueRouter({模式: 'history',routes:[...]}) 使用歷史記錄模式時,URL將顯示爲「正常」,例如 http://oursite.com/user/id。美麗!
又來了一個問題,雖然:由於我們的應用程序是一個單頁客戶 端應用程序,沒有適當的服務器配置,如果他們直接在他們的 瀏覽器訪問http://oursite.com/user/id的用戶將獲得一個 404錯誤。現在這很醜陋。
不用擔心:要解決該問題,您只需將簡單的 全備回退路線添加到您的服務器。如果該網址與任何 靜態資產都不匹配,則該網址應該與您的應用 所在的index.html頁面保持一致。再次美麗!
阿帕奇
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
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)
})
你能否詳細說明你的解決方案多一點? –