网址中未显示Angular Base href

时间:2019-08-21 10:27:41

标签: angular routing href angular-routing

我正在尝试将我的角度应用程序部署到生产环境中,该生产环境在url中具有附加的定位步骤,例如附加到它的www.production-server.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-my-app / page2。为什么会这样?

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

As seen on this picture

2 个答案:

答案 0 :(得分:0)

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

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

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

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

lite-server --baseDir="dist"

您可以从官方文档here中了解更多信息。

答案 1 :(得分:0)

尝试运行

$ ng build --prod --base-href="add/your/base/url/here"