【问题标题】:Angular 7: localhost route works, after deploy when update on route => not foundAngular 7:本地主机路由有效,在路由更新时部署后=>未找到
【发布时间】:2019-07-12 10:25:12
【问题描述】:

本地主机上的路由有效,我也可以从路由访问,而不仅仅是从索引 www.myhost.com/route1 在本地主机上工作 www.myhost.com => www.myhost.com/route1 在我的家庭服务器上工作(我有公共 ip) www.myhost.com/route1 不起作用 => 给我 NOT FOUND

我使用 ng build --prod --watch 构建 我尝试 PathLocationStrategy 我尝试 APP_BASE_HREF 我试试 LocationStrategy

base 设置为“/”

我尝试了 useHash,它可以工作,但真的很难看。

const appRoutes: Routes = [
  {path: '', pathMatch: 'full', component: MatchListComponent},
  {path: 'match/:id', component: MatchDetailComponent},
  {path: 'team/:id', component: TeamDetailComponent}
];

@NgModule({
  declarations: [
    AppComponent,
    MatchListComponent,
    MatchDetailComponent,
    TeamDetailComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    NgbModule,
    RouterModule.forRoot(appRoutes, {scrollPositionRestoration : 'enabled'})
  ],
  providers: [{provide: PathLocationStrategy}],
  bootstrap: [AppComponent]
})
export class AppModule { }

我尝试了一些角度 2 或 4 的解决方案,但它们不起作用。 我的网络服务器是 Apache2

【问题讨论】:

  • 那么 useHash 有用吗?

标签: angular deployment angular7 angular-router


【解决方案1】:

如果您使用 Spring(或其他一些端点技术),可以使用的另一个选项是定义一个控制器,将 Angular 应用程序中的所有路由重定向到 index.html 文件。示例:

@Controller
@RequestMapping({
  "/login",
  "/clients",
  "/admin/users",
  "/admin/programs",
  "/reports",
  "/404"
})
class AppController {
  @GetMapping
  String admin() {
    return "../static/index";
  }
}

【讨论】:

    【解决方案2】:

    我们可以通过使用哈希策略来克服这个问题

    希望对你有帮助。

     RouterModule.forRoot(routes, {
              useHash: true
            })
          ],
    

    【讨论】:

    • 我说,在代码之前,hashStrategy 真的很丑,对 SEO 不利
    • @Bluex 然后使用 web.config 文件这里是链接github.com/angular/angular-cli/issues/2885
    • 好的,我试试 web.config。如果它有效,我在这里写。我还需要“patchMatch:full”?
    • 是的,需要 pathMatch
    • 没什么。 Web.config 不起作用。如果我从一条路线导航到另一条路线没关系,但如果我直接在 www.mysite.it/match 之类的路线上进行访问,浏览器会给我“未找到请求的 URL /匹配未找到”。为什么简单的部署如此困难
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    相关资源
    最近更新 更多