angular - 在 Angular 中构建后无法加载路由模块

标签 angular xampp

我正在尝试构建我的 Angular 应用程序并在 xampp 上提供它,但是在构建我的 Angular 项目并将 dist 文件夹复制到 htdocs 文件夹后,我无法访问路由模块。 使用 ng serve 运行 Angular 应用程序时,访问模块中的路由没有问题,但在构建它之后,尝试访问路由模块中的路由时出现 404 错误。

应用模块中的路由工作得很好。

我的 app-routing.module.ts:

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


const routes: Routes = [
  {path: 'intern', loadChildren: () => import('./intern/intern.module').then(m => m.InternModule)}
];

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

我的实习生-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardSiteComponent } from './sites/dashboard-site/dashboard-site.component';


const routes: Routes = [
  {
    path: '', children: [
      { path: 'dashboard', component: DashboardSiteComponent }
    ]
  }
];

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

将以下 htaccess 文件添加到我的 htdocs 文件夹后,我不再收到 404 错误,但该页面只是空的。 .htaccess 文件:

RewriteEngine On

    # -- REDIRECTION to https (optional):
    # If you need this, uncomment the next two commands
    # RewriteCond %{HTTPS} !on
    # RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    # --

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d

    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) index.html [NC,L]

我也已经将 index.html 中的基本 href 更改为 <base href="./">但这也没有解决我的问题。

最佳答案

像这样使用 angular cli 构建您的应用

ng build --prod --base-href /<project_name>/

试试这个,让我知道它对我有用

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.html$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.html [L]
</IfModule>

关于angular - 在 Angular 中构建后无法加载路由模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62107162/

相关文章:

redirect - XAMPP - 如何在 htdocs 中设置网站的根文件夹?

xampp 中缺少 phpmyadmin

php - MySQL 数据库无法启动 XAMPP

angular - 在 Angular 9 中如何在 http 客户端响应模型中强制属性日期类型?

javascript - RxJs - 获取两个可观察对象的重复项

typescript - 在angular 2 typescript 中导入gapi.auth2

angular - 渲染器多个 selectRootElement 问题

mysql - #2002 - - 服务器没有响应(或本地服务器的套接字配置不正确)

php - 无法加载动态库 'php_libsodium'

javascript - 在 Angular 5 中使用 lodash.clonedeep 时出错