Angular base href not showing up in URL

假如想象 提交于 2019-12-11 15:54:46

问题


I am trying to deploy my angular application to a production environment that has an additional location step in url e.g. www.production-server.com/name-of-my-app appended to it.

My application works just fine when I run it through angular cli on localhost:4200 and redirects through pages like it is supposed to for example from localhost:4200/page1 to localhost:4200/page2.

But when I try to set the base href

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

and configure my Routes like this:

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 }
    };
  }

}

The application still routs localhost:4200/page1 to page localhost:4200/page2 instead of localhost:4200/name-of-my-app/page1 and localhost:4200/name-of-my-app/page2. Why is that?

EDIT: I would like to add that the base href is showing up correctly when I inspect the page. But it doesn't show up in the URL.

As seen on this picture


回答1:


The base href is used only in production. If you still want to see how it behaves before deployment you could try the following:

On the first terminal, run the ng build command in watch mode to compile the application to the dist folder:

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

On the second terminal, install a web server (such as lite-server), and run it against the output folder. For example:

lite-server --baseDir="dist"

You can read more about it from the official docs here.



来源:https://stackoverflow.com/questions/57589564/angular-base-href-not-showing-up-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!