0

I created my app using angular-cli.
I wrote service GameService for getting data from json file:

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class GameService {

  constructor(private http: Http) { }

  getGames() {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.get('app/shared/games.json', options)
        .map((res: Response) => res.json());
  }
}

Also I created model Game:

export class Game {
    title: String;
    image: String;
    cost: String;
}

I imported the service and the model in my AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

import { GameService } from './shared/services/game.service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpModule
  ],
  providers: [
    GameService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Loading application I get an error:

GET http://localhost:4200/app/shared/games.json 404 (Not Found)

Below is structure application:
enter image description here

I don't understand where I made the error exactly. I searched and saw many questions and answers to it on stackoverlow but it didn't help.
Please, help me.

Aleksei Korkoza
  • 427
  • 7
  • 22
  • 3
    You have to put the `shared` folder in `assets` object in `.angular-cli.json`. Alternatively, try placing the json file somewhere else such as the `assets` folder generated by default from angular-cli. – Edric Jul 05 '17 at 14:30
  • See the accepted answer here: https://stackoverflow.com/questions/40424907/whats-the-default-path-for-static-files-in-angular2 – Mark Jul 05 '17 at 14:34
  • Many thanks! I moved json file in the `assets` folder and now I don't get any errors. :) Good luck all. – Aleksei Korkoza Jul 05 '17 at 14:42

0 Answers0