6

if I build an angular2 app, I can easily go into my dev folder, double-click on my index.html and the browser will open the page and get all resources displaying the app right - without any local dev-server.

But when I add the <base href="/"> tag in index.html that I NEED for angular's router to work, the browser can't load the page anymore.

Using a dev-server this works fine.

Can someone explain me what's the reason for that ??

M.N. Bug
  • 253
  • 3
  • 11

2 Answers2

17

When you use <base href="/"> and open the index.html directly base is considered to be a file file:///C:/, or similar, depending on where your file is.

One solution is to use relative path: with <base href="./"> base will be current folder where your file is. You can use this if you have to run app from USB stick, for example

Sasxa
  • 40,334
  • 16
  • 88
  • 102
  • When I had `` my angular 2 solution was working locally with nodejs but when I copied the files over the IIS on our server I got 404's for all the scripts and css files in index. Adding the `.` before the `/` make it work. Thank you. – Kevin Quiring Jul 26 '16 at 21:41
0

I have a virtual directory in IIS 7.5 named /ang2 that points to C:\Apps\hello-world\dist. I am building the project with angular-cli (ie: "ng build --target=production" or "ng build target=development"). If I change base href to ./ it works. I also got it working with ./ang2. I have not tested any routing yet as this is a just a project created with the command "ng new hello-world".

howserss
  • 1,139
  • 1
  • 8
  • 12