ng build failed to execute 'replaceState' on 'History': A history state object with URL cannot be created in a document with origin 'null' and URL

萝らか妹 提交于 2019-12-11 01:57:19

问题


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:

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!


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/48398878/ng-build-failed-to-execute-replacestate-on-history-a-history-state-object-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!