0

I'm trying to run an Angular 2 application from a website where the base url should be http://www.example.com/app instead of http://www.example.com. I've tried several guides to change this by setting the href of the base element (such as this one: Angular 2 router no base href set), but none of these works. I can still only visit the pages on the root-url (thus http://www.example.com/something instead of http://www.example.com/app/something). This is how the index.html-file looks like at the moment:

<!doctype html>
<html>
  <head>
    <base href="/app">
    <title>Test</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">

    <!-- Bootstrap stylesheet -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <!-- Bootstrap (and jQuery) scripts -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

    <!-- Font Awesome icons! -->
    <script src="https://use.fontawesome.com/9fbdc2c47e.js"></script>
  </head>
  <body>
    <app-root>
      <div class="centered-container">
        <!-- Show animated loading icon -->
        <div id="loading-notification">
          <i class="fa fa-refresh fa-spin fa-5x fa-fw"></i>
        </div>
      </div>
    </app-root>
  </body>
</html>

What have I done wrong? An example of how my paths in the router look like:

RouterModule.forRoot([
      {
        path: "language",
        component: LanguageComponent
      },
      {
        path: "404",
        component: NotFoundComponent
      },
      {
        path: "**",
        component: NotFoundComponent
      }
    ]),

UPDATE:

I eventually fixed it! When building the application for production, the following command should be used: ng build --target=production --base-href /app/ --aot true. That fixes everything :)

Community
  • 1
  • 1
  • make the component which should be listed under the app.module.ts inside the bootstrap tag – skid Mar 14 '17 at 17:35

2 Answers2

1

you can make the following change under your routing

path: '', redirectTo: '/app', pathMatch: 'full'
path: 'app', component:'componentName'

which will eventually redirect you to the /app everytime when some error url is being given

skid
  • 958
  • 4
  • 13
  • 29
  • This is not really what I'm looking for. I want all components path to start with /app/ instead of using the server's root / –  Mar 14 '17 at 18:08
  • are you using systemjs else webpack config for the project – skid Mar 14 '17 at 19:13
0

Have you tried this

<base href="/app/">

and in your component

@Component({ moduleId: "app", selector:"my-component", ... })

null canvas
  • 10,201
  • 2
  • 15
  • 18
  • Ah, I will give it a shot :) Thanks –  Mar 14 '17 at 17:47
  • Hmmm, it's not working. When I go to http://www.example.com/app, it gives me a 404 and when I go to http://www.example.com everything works fine. Please note that I want everything (so all routes in the application) to start with /app –  Mar 14 '17 at 17:51
  • I also noticed that when the project gets compiled, the href in base is again changed to "/". –  Mar 14 '17 at 17:56