How to use "ng build -prod" and "ng serve -prod" with Angular 2 CLI, getting 404 errors

When I try to run ng build using the -prod option, it compiles to a single main.js file and I don't get any errors in the console.

But when I run the application in the browser, it is still looking for individual js files.

my main.ts:

// default
import { provide, enableProdMode, ExceptionHandler } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS, Http } from '@angular/http';

// External
import {
  TranslateService, TranslateLoader, TranslateStaticLoader
} from 'ng2-translate/ng2-translate';
import {Angulartics2} from 'angulartics2';

// mine
import { CustomExceptionHandler } from './app/_common/CustomExceptionHandler';
import { UserService } from './app/_services/user.service';
import { MessagesService } from './app/_services/messages.service';
import { APP_ROUTER_PROVIDERS } from './app/app.routes';
import { App, environment } from './app/';

if (environment.production) {
  enableProdMode();
}

bootstrap(App, [
  APP_ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  provide(ExceptionHandler, { useClass: CustomExceptionHandler }),
  provide(LocationStrategy, { useClass: HashLocationStrategy }),
  {
    provide: TranslateLoader,
    useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
    deps: [Http]
  },
  Angulartics2,
  TranslateService,
  MessagesService,
  UserService
])
.then(() => {
  console.log('Angular 2 loaded');
})
.catch(err => console.error(err));

When the application launches in the browser, it searches

app/_services/messages.service.js
app/_services/user.service.js
app/app.routes.js
etc...

All of these requests will receive 404, of course, since js files are compressed into a single main.js. How can I avoid this?

+4
source share
1 answer

, , , .

, , .

:

ng build -prod --base-href /tony/myproject/
+8

Source: https://habr.com/ru/post/1649270/


All Articles