角CLI的項目,在根文件夾我有一個文件lessons.ts(存儲數據)和app文件夾,我有教訓。 service.ts(檢索數據),代碼看起來像這樣:角2:http.get不明白TS擴展
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs';
//import Lesson
import { Lesson } from './lesson.class';
@Injectable()
export class LessonService {
lessons=[];
constructor(private http: Http) {
this.loadLessons();
}
loadLessons(){
this.http.get('./lessons')
.map(res=>res.json())
.subscribe(
lessons=>this.lessons =lessons,
err => console.log(err)
)
}
}
控制檯拋出一個錯誤消息:
zone.js:1382 GET http://localhost:4200/lessons 404 (Not Found)
修復它,我修改lessons.service.ts
.....
this.http.get('./lessons.ts') ->I added '.ts' for lessons file
...
但瀏覽器保持投擲錯誤:
zone.js:1382 GET http://localhost:4200/lessons.ts 404 (Not Found)
有沒有人遇到過這種錯誤? 任何想法,將不勝感激!
是什麼'/ lessons'? – micronyks
'./lessons'是要提交lessons.ts的網址。我放在根文件夾 – Albert
爲什麼你使用HTTP訪問它,如果它在你自己的應用程序的其他地方?我建議你閱讀例如https://angular.io/docs/ts/latest/tutorial/toh-pt4.html瞭解服務應該如何工作。實際上有一個使用TS文件中的靜態數據的例子。 – jonrsharpe