2

On my server I can´t put the Angular 2 files into the root folder. So my path is /another-folder. I put that into my index.html like this:

<base href="/another-folder">

When I open up the URL http://servername/another-folder the app does not load as the files can´t be found. The app searches for the files in the root. E.G. http://servername/main.bundle.js

Is there any other place where I have to define the base URL?

Max
  • 1,053
  • 1
  • 13
  • 34

2 Answers2

5
import {APP_BASE_HREF} from '@angular/common';

@NgModule({
  declarations: [AppComponent],
  imports: [routing /* or RouterModule */], 
  providers: [{provide: APP_BASE_HREF, useValue : '/' }]
]); 

See also Angular 2 router no base href set

Liam
  • 27,717
  • 28
  • 128
  • 190
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Ok. In my local environment it uses the new path. But on my server it still doesn´t find the resources. – Max Feb 20 '17 at 13:30
  • Sounds like your server doesn't support HTML5 pushState. See also http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser/36310728#36310728. You need to configure your server for HTML5 pushState. – Günter Zöchbauer Feb 20 '17 at 13:33
  • So i am doing a ng serve as I use Angular 2 CLI. And I have to configure pushState there somewhere? – Max Feb 20 '17 at 13:39
  • I assume "my server" is a different server than `ng serve`, and this server needs to be configured to support HTML5 pushState. Altenatively you can switch the Angular router to use `HashLocationStrategy`, then the server doesn't need to support pushState. – Günter Zöchbauer Feb 20 '17 at 13:41
  • In what file is that? – Erk Sep 28 '18 at 23:09
  • app.module . . . – Günter Zöchbauer Sep 29 '18 at 07:48
4

I think the trailing slash is missing

<base href="/another-folder/">

At least for me this is working without reverting to TypeScript

mtraut
  • 4,720
  • 3
  • 24
  • 33