VUE 配置history路由模式配置
vue-router 默認(rèn) hash 模式 —— 使用 URL 的 hash 來模擬一個(gè)完整的 URL,于是當(dāng) URL 改變時(shí),頁面不會(huì)重新加載。
如果不想要很丑的 hash,我們可以用路由的 history 模式,這種模式充分利用 history.pushState API 來完成 URL 跳轉(zhuǎn)而無須重新加載頁面。
const router = new VueRouter({
mode: 'history',
routes: [...]
})
當(dāng)你使用 history 模式時(shí),URL 就像正常的 url,例如 http://yoursite.com/user/id,也好看!
不過這種模式要玩好,還需要后臺(tái)配置支持。因?yàn)槲覀兊膽?yīng)用是個(gè)單頁客戶端應(yīng)用,如果后臺(tái)沒有正確的配置,當(dāng)用戶在瀏覽器直接訪問 http://oursite.com/user/id 就會(huì)返回 404,這就不好看了。
所以呢,你要在服務(wù)端增加一個(gè)覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態(tài)資源,則應(yīng)該返回同一個(gè) index.html 頁面,這個(gè)頁面就是你 app 依賴的頁面。
后端配置例子
注意:下列示例假設(shè)你在根目錄服務(wù)這個(gè)應(yīng)用。如果想部署到一個(gè)子目錄,你需要使用 Vue CLI 的 publicPath 選項(xiàng) (opens new window)和相關(guān)的 router base property (opens new window)。你還需要把下列示例中的根目錄調(diào)整成為子目錄 (例如用 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
對(duì)于 Node.js/Express,請(qǐng)考慮使用 connect-history-api-fallback 中間件 (opens new window)。
Internet Information Services (IIS)
安裝 IIS UrlRewrite(opens new window)
在你的網(wǎng)站根目錄中創(chuàng)建一個(gè) web.config 文件,內(nèi)容如下:
<?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 主機(jī)
在你的 firebase.json 中加入:
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
給個(gè)警告,因?yàn)檫@么做以后,你的服務(wù)器就不再返回 404 錯(cuò)誤頁面,因?yàn)閷?duì)于所有路徑都會(huì)返回 index.html 文件。為了避免這種情況,你應(yīng)該在 Vue 應(yīng)用里面覆蓋所有的路由情況,然后再給出一個(gè) 404 頁面。
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '*', component: NotFoundComponent }
]
})
或者,如果你使用 Node.js 服務(wù)器,你可以用服務(wù)端路由匹配到來的 URL,并在沒有匹配到路由的時(shí)候返回 404,以實(shí)現(xiàn)回退。更多詳情請(qǐng)查閱 Vue 服務(wù)端渲染文檔
proxytable
如果您在 Vue 中啟用了 History 模式,并且在瀏覽器中訪問路由時(shí),可能會(huì)遇到找不到 chunk.xxx.js 文件的問題。
這是因?yàn)樵?History 模式下,Vue Router 使用 HTML5 History API 來管理應(yīng)用程序的路由。當(dāng)用戶在瀏覽器中訪問應(yīng)用程序的路由時(shí),瀏覽器會(huì)向服務(wù)器發(fā)送請(qǐng)求,以獲取相應(yīng)的文件。如果服務(wù)器沒有正確地配置路由,或者沒有正確地處理路由請(qǐng)求,那么瀏覽器就會(huì)收到 404 錯(cuò)誤,無法加載相應(yīng)的 chunk 文件。
為了解決這個(gè)問題,您需要確保您的服務(wù)器正確地處理路由請(qǐng)求,并正確地返回相應(yīng)的文件。如果您正在使用 webpack 打包您的應(yīng)用程序,那么您需要確保在 webpack 配置中設(shè)置了正確的 publicPath 選項(xiàng)。publicPath 選項(xiàng)指定了 webpack 在打包時(shí)生成的文件的 URL 前綴,以便瀏覽器可以正確地加載這些文件。
例如,如果您的應(yīng)用程序部署在根目錄下,那么您可以將 publicPath 設(shè)置為:
module.exports = {
// ...
output: {
publicPath: '/',
// ...
},
// ...
}
如果您的應(yīng)用程序部署在子目錄下,例如 /my-app/,那么您需要將 publicPath 設(shè)置為該子目錄的路徑:文章來源:http://www.zghlxwxcb.cn/news/detail-495095.html
module.exports = {
// ...
output: {
publicPath: '/my-app/',
// ...
},
// ...
}
需要注意的是,在設(shè)置 publicPath 選項(xiàng)時(shí),您需要將其設(shè)置為實(shí)際部署應(yīng)用程序的 URL 路徑。如果您在開發(fā)環(huán)境下使用了代理服務(wù)器(ProxyTable),那么您需要在代理服務(wù)器上設(shè)置正確的重寫規(guī)則,以確保請(qǐng)求被正確地轉(zhuǎn)發(fā)到您的應(yīng)用程序。文章來源地址http://www.zghlxwxcb.cn/news/detail-495095.html
到了這里,關(guān)于VUE 配置history路由模式配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!