问题
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