![](/img/trans.png)
[英]Angular route with html5Mode giving 'Not found' page after reload
[英]html5mode Angular giving exception
我正在Angular 1.5.5中構建一個網站,並希望擁有漂亮的url(不帶#)。 按照其他文章的說明,我在index.html中添加了Html5mode
作為路由器以及基本url。
$locationProvider.html5Mode(true).hashPrefix('!');
<base href="innovationglobal">
但是,我得到以下異常:
錯誤:無法在“歷史記錄”上執行“ replaceState”:無法在源為“ http:// localhost:8080 ”的文檔中創建URL為“ http:// home / ”的歷史狀態對象。 在Yf.k.url(lib / 1.5.5 / angular.min.js:47:311)出現錯誤(本機)時
將標簽更改為絕對URL或路徑后
<base href="innovationglobal">
通過添加/
或https://
<base href="/innovationglobal/">
現在,當您重新加載頁面時,瀏覽器將向服務器發送您當前路徑的請求: /innovationglobal/
您需要設置服務器以處理該路徑-以及Angular應用可能放入瀏覽器地址欄中的任何其他路徑。 可能是/innovationglobal/users/
或/innovationglobal/products/detail/
等。
因為用戶可以按F5
重新加載他們正在查看的任何頁面。 這將使您的Angular應用程序失去控制權,並將對該URL的請求發送到服務器。
因此,您需要為服務器設置全部功能。 無論地址欄中的路徑如何,您的服務器都應使用Angular應用程序的index.html
進行回復。
但是要小心,您需要為靜態資產(.js和.css之類的文件)設置一個例外。 對於nginx,這看起來像這樣:
location = /static/ {
alias /var/www/static/;
try_files $uri $uri/ =404;
}
location /innovationglobal/ {
root /var/www/innovationglobal;
try_files $uri /app/index.html =404;
}
根據您使用的Web服務器。
您是否嘗試過html5Mode
不使用基礎的情況下啟用html5Mode
:
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.