我正在使用 Tomcat 來通過路由器托管 Angular 應用程式。但是當我嘗試使用路由時,我收到了 404 錯誤(主要/入門)。
Tomcat 在 localhost:9443/main 上運行。
路由器配置:
{path: 'getting-started', component: GettingStartedComponent},
{path: '', component: codeComponent},
{path: 'main', component: codeComponent}
主要作業正常。ng serve 的入門作業。
試過這個解決方案,沒有用:https ://medium.com/@nithin.biliya/deploying-angular-application-on-tomcat-server-fixing-deep-linking-issue-577565fe303d
在角度配置和 index.html 中嘗試了不同的 baseHref。
根據https://angular.io/guide/deployment,
路由應用程式必須回退到 index.html
如果應用程式使用 Angular 路由器,您必須將服務器配置為在請求提供它沒有的檔案時回傳應用程式的主機頁面 (index.html)。
路由應用程式應支持“深層鏈接”。
現在,我怎樣才能在tomcat上設定它?
uj5u.com熱心網友回復:
對于其他對此問題有疑問的人。它是由
如果應用程式使用 Angular 路由器,您必須將服務器配置為在請求提供它沒有的檔案時回傳應用程式的主機頁面 (index.html)。
對我有用的解決方案:
在您的 Eclipse 專案中,在 Tomcat 上提供有角度的構建:
在 META-INF 檔案夾中編輯檔案 context.xml 并添加 RewriteValve:
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
如果您沒有 context.xml - 創建它并添加以下 xml:
<?xml version="1.0" encoding="utf-8"?>
<Context>
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Context>
在 WEB-INF 中,創建檔案rewrite.config(該檔案包含 URL 重寫的規則,并被 tomcat 用于 URL 重寫)。在 rewrite.config 中,復制以下內容:
RewriteCond %{SERVLET_PATH} !-f
RewriteRule ^/(.*)$ /index.html
當被要求提供一個它沒有的檔案時,這將回傳應用程式的主機頁面 (index.html)。
現在,如果問題沒有解決 - 根據您的專案設定,您可能需要更改 angular.json 中的 baseHref 或更改 RewriteRule。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/530840.html
標籤:有角度的雄猫路线
