【问题标题】:It gives me an error 404 Not Found Angular它给了我一个错误 404 Not Found Angular
【发布时间】:2020-10-24 00:45:30
【问题描述】:

我有一个登录页面和另一个主页,问题是在生产中如果您不通过登录,您将无法进入主页,也就是说......如果我这样做了

ng build --prod --base-href /project/

在生产中,如果我访问 http://xx.xxx.xxx.x/project 它可以工作,但如果我在导航栏中输入 http://xx.xxx.xxx.x/project/homehttp://xx.xxx.xxx.x/project/login 给出未找到 404

如果我在登录或主页中并且我重新加载页面,它也会给我同样的错误,这让我再次输入路由:http://xx.xxx.xxx.x/project

通过登录进入主页。通过执行 ng 服务,这在本地对我很有效,但在生产中却不行。

我的路由代码:

const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'login'
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: '',
    component: MainLayoutComponent,
    canActivateChild: [AuthGuard],
    children: [
      {
        path: 'home',
        component: HomeComponent,
        canActivate: [AuthGuard]
      },
      {
        path: 'config',
        component: ConfigComponent,
        canActivate: [AuthGuard]
      },
    ]
  },
];

【问题讨论】:

    标签: angular routes url-routing


    【解决方案1】:

    将以下代码添加到 app.module.ts 中

    import { PathLocationStrategy, LocationStrategy } from '@angular/common';
    // In providers
    providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2013-03-10
      • 2016-08-29
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多