刷新页面时如何防止 Angular 出现 404 not found 错误?

How can I prevent 404 not found error in Angular when I refresh page?

我有一个 Angular 项目,当我将它上传到全球服务器时,它 returns 在我刷新页面时出现 404 not found 错误。我怎样才能避免这种情况?

那是我的 routing.component.ts

import { RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';

import { PagesComponent } from './pages.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { ECommerceComponent } from './e-commerce/e-commerce.component';
import { NotFoundComponent } from './miscellaneous/not-found/not-found.component';
import { createFalse } from 'typescript';

const routes: Routes = [
  
  
  {

  path: '',
  component: PagesComponent,
  children: [
    {
      path: 'dashboard',
      component: ECommerceComponent,
      
    },
    {
      path: 'iot-dashboard',
      component: DashboardComponent,
      
    },
    
    
    {
      path: '',
      redirectTo: 'iot-dashboard',
      pathMatch: 'full',
    },
    
    {
      path: '**',
      component: NotFoundComponent,
    },
  ],

}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class PagesRoutingModule {
}

您必须在服务器上设置回退到 index.html(您没有说是哪个:Apache HTTPD?Nginx?): https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml

只需使用 angular 的 HashLocationStrategy。

RouterModule.forRoot(routes, {useHash: true});