app.routing.module.ts
import { NgModule } from '@angular/core'; import { Routes,RouterModule } from '@angular/router'; import { HomeComponent } from './public/home/home.component'; const routes: Routes = [ { path: 'customers',loadChildren: '../app/customers/customers.module#CustomersModule' },{ path: 'admin',loadChildren: '../app/admin/admin.module#AdminModule' },{ path: 'home',component: HomeComponent },{ path: '**',redirectTo: 'home',} ]; @NgModule({ imports: [RouterModule.forRoot(routes)],exports: [RouterModule] }) export class AppRoutingModule { }
这些在开发环境下工作正常.
如果我直接打开localhost:4200 / home,它就会登陆到预期的页面.
但是,如果我直接打开www.domain.com/myngapp/home,那么使用ng build –prod –base-href = / myngapp /的内置和部署只能在www.domain.com/myngapp上运行,然后它给出了404未找到的错误.
解决方法
您需要设置正确的URL重写规则. Angular docs解释了如何为大多数流行的http服务器执行此操作.你可以在这里找到细节.
https://angular.io/guide/deployment#production-servers
它的作用就是简单地说,无论请求的路径是什么,都要告诉服务器始终提供index.html.例如.
www.domain.com/myngapp/home – >服务index.html,而不是home.html或类似的东西www.domain.com/myngapp/some/child/routing – >提供index.html并且不要试图找到一些/ child / routing.html或PHP等等.