Angular2 not loading when directly browsed to route address or on refresh

折月煮酒 提交于 2019-12-11 05:58:14

问题


In the application there is a route defined:

const routes: Routes = [
  { path: '', component: MainComponent },
  { path: 'users', component: UsersComponent }
]

In index.html the base href is also correctly defined.

<base href="/">

When navigating through application the routes are loaded correctly. But when users enters .../users directly through the browser the application is not loaded and returns 404. Also when the users refresh the webpage (F5), the application is also not loaded.

Is there something wrong with angular code or server configuration?


回答1:


It depends on your server setup. You need to either set up a redirect to index.html for when a 404 occurs,

Or set up a rewrite so that every request after a certain path always leads to index.html,

Or simply change the location strategy to hash instead of HTML5 (although not what I'd recommend for a few reasons, as Peter said).



来源:https://stackoverflow.com/questions/41351052/angular2-not-loading-when-directly-browsed-to-route-address-or-on-refresh

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