Angular 部署 - 页面刷新 404

标签 angular

我想在 Angular 中将我的应用程序部署到生产服务器,但我遇到了问题。当我只使用 Angular 路由(更改组件,而不是重定向)时,应用程序可以正常工作,但是当我在浏览器中刷新页面时,我得到一个从 IIS 返回的 404 页面(我使用 IIS 作为 Web 服务器)

这是我的 Angular 路由:

const appRoutes: Routes = [
    { path: '', redirectTo: '/home', pathMatch: 'full', canActivate: [AuthGuard] },
    { path: 'home', component: DashboardComponent, canActivate: [AuthGuard] },
    { path: "profile", component: UserProfileComponent, canActivate: [AuthGuard] },
    { path: '400', component: ErrorComponent },
    { path: '401', component: ErrorComponent },
    { path: '403', component: ErrorComponent },
    { path: '404', component: ErrorComponent },
    { path: '500', component: ErrorComponent },
    { path: '503', component: ErrorComponent },
    { path: '**', redirectTo: '/404' }
]

最佳答案

这适用于 angular 8,将 .htaccess 添加到您的项目文件中

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]

关于Angular 部署 - 页面刷新 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072185/

相关文章:

angular - Angular 2 中同一根应用程序的多个实例

Angular 5 向一组组件发送 @Input

angular - 在 Angular2 中,服务值是如何返回的?

Angular 2如何在功能模块中隔离服务提供者

angular - 无法在 Angular 多重选择中预选值

angular - 移动目标元素时不会触发点击处理程序

Angular Testing : using fakeAsync with async/await

java - 错误 DOMException : Failed to execute 'open' on 'XMLHttpRequest' : Invalid URL

Angular 2 - CanActivate Observable 在订阅后获得值(value)

html - Angular Heroes 教程 <app-heroes> css 选择器