Angular 2 routes not working after building the project

[亡魂溺海] 提交于 2019-12-05 17:58:54

You need to make sure if you're serving your app via a express server or any other web server , you should redirect all the get requests to index.html.

As long as your server is not redirecting all the requests to index.html, it doesn't matter what's happening inside your Angular app.

As @Milad stated I had to redirect all get request to index.html. Adding .htacces file solved my issue:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
</IfModule>

If you want to redirect, you should specify exactly that:

  { path: '**', redirectTo: '/'}

However, I recommend something like:

  {path: '/welcome', component: WelcomeComponent},
  {path: '/login', component: LoginComponent},
  {path: '**', redirectTo: '/welcome'}

Also note the leading slashes in the path.

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