2

I am using Google Chrome version 52 and I want to be able to run my application locally without a server by accessing index.html.

I am able to hit the landing page of my application. However, when I try to navigate to another page or click on any buttons, it is unable to work.

I have tried many ways, such as:

  • Add script before import reference one and reference two
  • Changing base href from / to ./
  • Changing router configurations to use hash and changing base href to <script>document.write('<base href="' + document.location + '" />');</script>
  • Using ng build --prod --allow-file-access-from-files

This is the exact error I faced: enter image description here

I have been trying for a day, would really appreciate if someone could help.

I do not wish to run a web server and I have to make it work on Chrome.

Edited: Works on Safari

Does not work on Chrome, Firefox, Opera, IE, Microsoft Edge

Thank you!

JiiB
  • 1,432
  • 1
  • 12
  • 26
icedmilocode
  • 95
  • 1
  • 2
  • 15

3 Answers3

0

There a a few reasons, that could cause problems with your router. If you experience problems in your production environment, you should make sure, that you follow these steps:

Step 1:

Add a base tag in the header section of your index.html:

<base href="/">

Step 2:

Createa .htaccess file in your root directory and set the ErrorDocument on your index.html, like in the example below.

RewriteEngine On

ErrorDocument 404 /

This should fix your issues.

Explanation:

When you load a page, e. g. domain.com/settings, your browser sends a request to the server and expects a settings.html file in response.

If the ErrorDocument points to the index.html file, the Angular router can handle the request correctly and will redirect you to domain.com/settings without getting a error.

JiiB
  • 1,432
  • 1
  • 12
  • 26
  • I have tried according to what you said and place the .htaccess file in my dist folder, does not work. The error is exactly the same as the screenshot I provided above. – icedmilocode Jan 25 '18 at 01:23
0

Ended up I use Web Server for Chrome to host a simple local server, which was not what I wanted but it's better than nothing.

Still hoping if anyone could solve this.

Thank you!

icedmilocode
  • 95
  • 1
  • 2
  • 15
0

Use hash location strategy and remove the base tag in index.html entirely.

greenPostIt
  • 63
  • 2
  • 6