Angular 基本 href 未显示在 URL 中

2023-12-20

我正在尝试将我的角度应用程序部署到生产环境,该环境在 url 中具有额外的位置步骤,例如www.生产服务器.com/name-of-my-app 附加到其后。

当我通过 localhost:4200 上的 Angular cli 运行它并通过页面重定向时,我的应用程序工作得很好,例如从 localhost:4200/page1 到 localhost:4200/page2。

但是当我尝试设置基本 href 时

 "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "baseHref" : "/name-of-my-app/",
            "outputPath": "dist",
            "index": "src/index.html",
...

并像这样配置我的路线:

const routes: Routes = [
  Route.withShell([
    {path: '', redirectTo: '/sredstva', pathMatch: 'full'},
    {path: 'sredstva', component: OsebnasredstvaComponent, data: {title: extract('Osebna sredstva')}}
  ])
];


/**
 * Provides helper methods to create routes.
 */
export class Route {

  /**
   * Creates routes using the shell component and authentication.
   * @param routes The routes to add.
   * @return {Route} The new route using shell as the base.
   */
  static withShell(routes: Routes): ngRoute {
    return {
      path: '',
      component: ShellComponent,
      children: routes,
      canActivate: [AuthenticationGuard],
      // Reuse ShellComponent instance when navigating between child views
      data: { reuse: true }
    };
  }

}

应用程序仍将 localhost:4200/page1 路由到页面 localhost:4200/page2,而不是 localhost:4200/name-of-my-app/page1 和 localhost:4200/name-of-my-app/page2。这是为什么?

编辑:我想补充一点,当我检查页面时,基本 href 显示正确。但它不会显示在 URL 中。

正如这张图片所示 https://i.stack.imgur.com/dVXMF.jpg


基本 href 仅在生产中使用。如果您仍然想在部署之前查看它的行为方式,可以尝试以下操作:

在第一个终端上,在监视模式下运行 ng build 命令将应用程序编译到 dist 文件夹:

ng build --watch --outputPath=outputPath --baseHref=baseHref

在第二个终端上,安装 Web 服务器(例如 lite-server),然后针对输出文件夹运行它。例如:

lite-server --baseDir="dist"

您可以从官方文档中了解更多相关信息here https://angular.io/guide/deployment.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 基本 href 未显示在 URL 中 的相关文章

随机推荐