Angular4:Post 部署问题
Angular4: Post deployment issue
我构建了一个 Angular4 应用程序,一切正常,但部署后我遇到了一个奇怪的问题。
我有 3 条路线:
/
: 对于 AppComponent
/login
: 对于 LoginComponent
/editor
:为了我的 EditorComponent
在开发模式下,当我在导航器的 url 栏中写入时 localhost://4200/login
或 localhost://4200/editor
工作正常但是当我在部署后执行时我得到 404 Page not found
!!
但是当我像<a routerLink="/login">Login</a>
一样使用Angular的routerLink
并点击它时,我成功地重定向到LoginComponent
!!
为什么(部署后)当我使用 routerLink 时一切正常,但如果我尝试直接从导航器的 url 栏访问组件,我会得到 404 Page not found
?!!
刷新或直接访问您的应用程序时出现 404 错误,因为浏览器中的实际地址正在更新(没有 # 方法)。
If you want not having a 404 error, you need to update your server to
serve the index.html file for each route path you defined.
如果你想切换到HashBang方式,你需要使用这个配置:
在AppModule.ts
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
providers:[
{provide: LocationStrategy, useClass: HashLocationStrategy}
]
在这种情况下,当您刷新页面时,它会再次显示(但您的地址中会有一个#)。
我构建了一个 Angular4 应用程序,一切正常,但部署后我遇到了一个奇怪的问题。
我有 3 条路线:
/
: 对于 AppComponent
/login
: 对于 LoginComponent
/editor
:为了我的 EditorComponent
在开发模式下,当我在导航器的 url 栏中写入时 localhost://4200/login
或 localhost://4200/editor
工作正常但是当我在部署后执行时我得到 404 Page not found
!!
但是当我像<a routerLink="/login">Login</a>
一样使用Angular的routerLink
并点击它时,我成功地重定向到LoginComponent
!!
为什么(部署后)当我使用 routerLink 时一切正常,但如果我尝试直接从导航器的 url 栏访问组件,我会得到 404 Page not found
?!!
刷新或直接访问您的应用程序时出现 404 错误,因为浏览器中的实际地址正在更新(没有 # 方法)。
If you want not having a 404 error, you need to update your server to serve the index.html file for each route path you defined.
如果你想切换到HashBang方式,你需要使用这个配置:
在AppModule.ts
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
providers:[
{provide: LocationStrategy, useClass: HashLocationStrategy}
]
在这种情况下,当您刷新页面时,它会再次显示(但您的地址中会有一个#)。