我使用angular-cli
创建了我的应用
我编写了服务GameService
来获取json文件中的数据:
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());
}
}
我也创建了模型Game
:
export class Game {
title: String;
image: String;
cost: String;
}
我在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 { }
加载应用程序我收到错误:
GET http://localhost:4200/app/shared/games.json 404 (Not Found)
我不明白我在哪里做错了。我在stackoverlow上搜索并看到了很多问题和答案,但它没有帮助 请帮我。