代码之家  ›  专栏  ›  技术社区  ›  Supun Abesekara

Angular URL在构建项目中不起作用,但在本地主机4200上起作用。

  •  0
  • Supun Abesekara  · 技术社区  · 6 年前

    当我打开子URL时,它在我的本地环境中工作,比如 http://localhost:4200/login . 但在我用 ng build --prod 所有子URL都无法在Live服务器上工作。

    如果我使用 this.router.navigate(['/system']); 它在构建的项目中工作,但是如果我重新加载同一个URL,它将不工作(404)。

    我的路由策略有什么问题吗?

    索引文件

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>title</title>
      <base href="/">
    </head>
    <body>
    
      <app-root></app-root>
    
    </body>
    </html>
    

    App.M.模块

    import {BrowserModule} from '@angular/platform-browser';
    import {NgModule} from '@angular/core';
    import {FormsModule} from "@angular/forms";
    import {AppRoutingModule} from './app-routing.module';
    import {AppComponent} from './app.component';
    import {HomeComponent} from './components/home/home.component';
    import {HttpClientModule} from "@angular/common/http";
    import {SystemComponent} from './components/system/system.component';
    import {RouterModule, Routes} from '@angular/router';
    import {AdminComponent} from './components/admin/admin.component';
    
    const appRoutes: Routes = [
        {path: '', component: HomeComponent}, // home pages
        {path: 'login', component: HomeComponent},// sub page 1
        {path: 'system', component: SystemComponent}, // sub page 2
        {path: 'admin', component: AdminComponent} //sub page 3
    ];
    
    @NgModule({
        declarations: [
            AppComponent,
            HomeComponent,
            SystemComponent,
            AdminComponent
        ],
        imports: [RouterModule.forRoot(
            appRoutes,
            {enableTracing: true},// <-- debugging purposes only it will show  big  console  log  data
            {useHash: true}  ),
            BrowserModule,
            AppRoutingModule,
            FormsModule,
            HttpClientModule
        ],
        providers: [],
        bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Dhananjai Pai    6 年前

    index.html

    ng build --base-href /

    ng build --bh /

    ng build --prod --bh /

    --deploy-url https://shekhargulati.com/2017/07/06/angular-4-use-of-base-href-and-deploy-url-build-options/

        2
  •  0
  •   Supun Abesekara    6 年前

    HOWTOs / Making sure .htaccess and mod_rewrite are working as they should

    • sudo nano /etc/apache2/sites-available/000-default.conf
    • DocumentRoot /var/www/html

        <Directory /var/www/html/projectFolder>
           # Don't show directory index
           Options -Indexes +FollowSymLinks +MultiViews
              
           # Allow .htaccess files
            AllowOverride All
              
           # Allow web access to this directory
           Require all granted
        </Directory>