VUE配置history路由模式配置詳細舉例
VUE 配置history路由模式配置
vue-router 默認 hash 模式 —— 使用 URL 的 hash 來模擬一個完整的 URL,于是當 URL 改變時,頁面不會重新加載。
如果不想要很丑的 hash,我們可以用路由的 history 模式,這種模式充分利用 history.pushState API 來完成 URL 跳轉而無須重新加載頁面。
const router = new VueRouter({
mode: 'history',
routes: [...]
})
當你使用 history 模式時,URL 就像正常的 url,例如 http://yoursite.com/user/id,也好看!
不過這種模式要玩好,還需要后臺配置支持。因為我們的應用是個單頁客戶端應用,如果后臺沒有正確的配置,當用戶在瀏覽器直接訪問 http://oursite.com/user/id 就會返回 404,這就不好看了。
所以呢,你要在服務端增加一個覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態(tài)資源,則應該返回同一個 index.html 頁面,這個頁面就是你 app 依賴的頁面。
后端配置例子
注意:下列示例假設你在根目錄服務這個應用。如果想部署到一個子目錄,你需要使用 Vue CLI 的 publicPath 選項 (opens new window)和相關的 router base property (opens new window)。你還需要把下列示例中的根目錄調整成為子目錄 (例如用 RewriteBase /name-of-your-subfolder/ 替換掉 RewriteBase /)。
Apache
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<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 (opens new window)。
nginx
location / {
try_files $uri $uri/ /index.html;
}
node
#原生 Node.js
const http = require('http')
const fs = require('fs')
const httpPort = 80
http.createServer((req, res) => {
fs.readFile('index.html', 'utf-8', (err, content) => {
if (err) {
console.log('We cannot open "index.html" 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)
})
基于 Node.js 的 Express
對于 Node.js/Express,請考慮使用 connect-history-api-fallback 中間件 (opens new window)。
Internet Information Services (IIS)
安裝 IIS UrlRewrite(opens new window)
在你的網站根目錄中創(chuàng)建一個 web.config 文件,內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Handle History Mode and custom 404/500" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Caddy
rewrite {
regexp .*
to {path} /
}
Firebase 主機
在你的 firebase.json 中加入:
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
給個警告,因為這么做以后,你的服務器就不再返回 404 錯誤頁面,因為對于所有路徑都會返回 index.html 文件。為了避免這種情況,你應該在 Vue 應用里面覆蓋所有的路由情況,然后再給出一個 404 頁面。
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '*', component: NotFoundComponent }
]
})
或者,如果你使用 Node.js 服務器,你可以用服務端路由匹配到來的 URL,并在沒有匹配到路由的時候返回 404,以實現回退。更多詳情請查閱 Vue 服務端渲染文檔
proxytable
如果您在 Vue 中啟用了 History 模式,并且在瀏覽器中訪問路由時,可能會遇到找不到 chunk.xxx.js 文件的問題。
這是因為在 History 模式下,Vue Router 使用 HTML5 History API 來管理應用程序的路由。當用戶在瀏覽器中訪問應用程序的路由時,瀏覽器會向服務器發(fā)送請求,以獲取相應的文件。如果服務器沒有正確地配置路由,或者沒有正確地處理路由請求,那么瀏覽器就會收到 404 錯誤,無法加載相應的 chunk 文件。
為了解決這個問題,您需要確保您的服務器正確地處理路由請求,并正確地返回相應的文件。如果您正在使用 webpack 打包您的應用程序,那么您需要確保在 webpack 配置中設置了正確的 publicPath 選項。publicPath 選項指定了 webpack 在打包時生成的文件的 URL 前綴,以便瀏覽器可以正確地加載這些文件。
例如,如果您的應用程序部署在根目錄下,那么您可以將 publicPath 設置為:
module.exports = {
// ...
output: {
publicPath: '/',
// ...
},
// ...
}
如果您的應用程序部署在子目錄下,例如 /my-app/,那么您需要將 publicPath 設置為該子目錄的路徑:
module.exports = {
// ...
output: {
publicPath: '/my-app/',
// ...
},
// ...
}
需要注意的是,在設置 publicPath 選項時,您需要將其設置為實際部署應用程序的 URL 路徑。如果您在開發(fā)環(huán)境下使用了代理服務器(ProxyTable),那么您需要在代理服務器上設置正確的重寫規(guī)則,以確保請求被正確地轉發(fā)到您的應用程序。
總結
到此這篇關于VUE配置history路由模式配置的文章就介紹到這了,更多相關VUE配置history路由模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue3使用setup監(jiān)聽props實現方法詳解
這篇文章主要為大家介紹了Vue3使用setup監(jiān)聽props實現方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
antd?Vue實現Login登錄頁面布局案例詳解?附帶驗證碼驗證功能
這篇文章主要介紹了antd?Vue實現Login登錄頁面布局案例詳解附帶驗證碼驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

