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



我有一个Angular项目,当我把它上传到全局服务器上时,当我刷新页面时,它会返回一个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 {
}

就使用angular的HashLocationStrategy吧。

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

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

相关内容

  • 没有找到相关文章

最新更新