在 Angular 中构建后无法加载路由模块 [英] Cannot load routing modules after building in Angular

查看:69
本文介绍了在 Angular 中构建后无法加载路由模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build my angular app und serve it on xampp, but after building my Angular Project and copying the dist folder into the htdocs folder, I cannot access the routing modules. When running the angular app with ng serve, I have no problems accessing the route in the module, but after building it, I am getting an 404 error when trying to acess a route thats in the routing module.

应用模块中的路由工作正常.

The routes that are in the app module work just fine.

我的 app-routing.module.ts:

My 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 { }

我的实习路由.module.ts:

My intern-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 文件:

After I added the following htaccess file to my htdocs folder I am not getting an 404 error any more but the page is just empty. .htaccess file:

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="./"> 但这也没有解决我的问题.

I have also already changed the base href in index.html to <base href="./"> but this didn't solve my problem as well.

推荐答案

使用 Angular cli 像这样构建您的应用程序

Build your app like this using angular cli

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

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

Try with this one and let me know as It works for me

<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 中构建后无法加载路由模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆