ng build failed to execute 'replaceState'

2019-07-11 04:28发布

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!

2条回答
做个烂人
2楼-- · 2019-07-11 04:42

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!

查看更多
走好不送
3楼-- · 2019-07-11 04:46

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.

查看更多
登录 后发表回答