首页 > 解决方案 > 部署在本地 tomcat 服务器上的 Angular 应用程序的 url 在将 url 粘贴到浏览器的新选项卡上时不会加载组件

问题描述

我已经在本地tomcat服务器中部署了一个角度应用程序并连接到一个使用身份服务器进行单点登录的spring应用程序,当我在eclipse上运行应用程序服务器并启动客户端应用程序时,应用程序可以无缝运行,但是当我复制 url 并将其粘贴到应用程序无法加载组件的浏览器的新选项卡上。

之前在浏览器的新标签上粘贴的 url 使用返回 404 错误,所以我搜索并发现我必须使用位置策略,并上传了一个新的 dist 文件,从那时起 url 不返回 404 而是无法加载(一个)组件。

这来自 app.routing.ts:

const routes: Routes = [
{ path: 'login', component: LoginComponent },
{path:'nav', component:NavBarComponent,
children:[
{path:'config', component:ConfigureComponent,
children:[
{path:'tenant', component:TenantComponent},
{path:'department', component:DepartmentComponent},
{path:'adminUser', component:DepartmentUserComponent},
{path:'setting', component:AdminSettingComponent},
{path: 'version', component: VersionManagementComponent},  
{path:'eventType',component:EventTypeComponent},
{path:'workforceUser',component:WorkforceUserComponent},
{path:'dashboard',component:DashboardComponent},
{path:'eform',component:EFormComponent},
{path:'groups',component:GroupsComponent},
{path:'userProfile',component:UserProfileComponent}
 ]},
]},
{ path: '**', component: LoginComponent },
]; 

ConfigureComponent 是不加载的。

在 app.module.ts 实现定位策略:

import { Location, HashLocationStrategy, LocationStrategy } from 
'@angular/common';

providers: [
 {provide:LocationStrategy, useClass: HashLocationStrategy},
HttpClientService,
AppConfigService,
toastrService,
WorkforceUserService,
AuthGuard
]

我应该在路由中进行哪些更改,以便在我将 url 复制并粘贴到新选项卡上时不会错过 ConfigureComponent,当我刷新浏览器时也会发生同样的事情。

标签: angularspring-mvctomcat

解决方案


您必须重写服务器的路由,以便将所有路由重定向到 index.html


推荐阅读