Cannot load routing modules after building in Angular

萝らか妹 提交于 2021-01-20 07:38:06

问题


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.

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

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

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]

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


回答1:


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>


来源:https://stackoverflow.com/questions/62107162/cannot-load-routing-modules-after-building-in-angular

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!