routing and clean path (no hashtag) not working in angularJS

后端 未结 3 890
[愿得一人]
[愿得一人] 2021-01-21 22:57

Guys this is driving me crazy, been fighting for hours on this, and couldn\'t find a solution!

I know that in order to clean the hashtag from the URL path, i need to use

相关标签:
3条回答
  • 2021-01-21 23:24

    I haven't tried myself yet, but from what I know, you have to make sure that your server simply returns your initial HTML page for these non-existing URLs. Then when the page will reload, an onload event will be fired and you can access the last state via history.state. I guess angular will just restore the state automatically so you shouldn't have to worry about this.

    0 讨论(0)
  • 2021-01-21 23:24

    I resolve this issue by adding a in web.xml

    When you refresh the target page manually, will redirect the 404page to your main page. And then angular router will take in effect to route this to target page.

    0 讨论(0)
  • 2021-01-21 23:37

    When you hit F5, you're not using an angular directive or anything angular-related. You simply tell the browser to send a new request to the URL that is currently in the address bar, and to refresh the page with what comes back from the server. So if the current location is http://localhost:8080/mywork, a request will be sent to http://localhost:8080/mywork, and if the server doesn't have anything configured to handle this URL, you'll get a 404.

    Now, if you configure the server to return the main angular HTML page when the URL /mywork is hit, then the browser will display this HTML page and execute the JavaScript it contains. The angular router will detect that the location is /mywork, and it will thus load the partial and the controller associated with this location, and display the appropriate template.

    The old, hashbang-based routing doesn't need any configuration of the backend because the URL is always the same. Only the part after the hashbang (unknown by the server), changes.

    0 讨论(0)
提交回复
热议问题