可觀察類型錯誤:無法讀取未定義的屬性在我的Angular 2應用程序中,我收到一個錯誤:無法讀取undefined的屬性'title'。這是一個非常簡單的組件,只是試圖在這里工作。它擊中我的API控制器(奇怪地多次),它似乎在返回一個對象后點擊回調。我的console.log輸出了我期望的對象。這是完整的錯誤:TypeError: Cannot read property 'title' of undefined
at AbstractChangeDetector.ChangeDetector_About_0.detectChangesInRecordsInternal (eval at <anonymous> (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:10897:14), <anonymous>:31:26)
at AbstractChangeDetector.detectChangesInRecords (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8824:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8807:12)
at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8877:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8811:12)
at AbstractChangeDetector._detectChangesContentChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8871:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8808:12)
at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8877:14)
at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8811:12)
at AbstractChangeDetector.detectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8796:12)該服務(about.service.ts):import {Http} from 'angular2/http';import {Injectable} from 'angular2/core';import {AboutModel} from './about.model';import 'rxjs/add/operator/map';@Injectable()export class AboutService {
constructor(private _http: Http) { }
get() {
return this._http.get('/api/about').map(res => {
console.log(res.json()); // I get the error on the line above but this code is still hit.
return <AboutModel>res.json();
});
}}
3 回答

慕仙森
TA貢獻1827條經驗 獲得超8個贊
它看起來像你about.title
在視圖中引用,about.html
但about
只有在http
請求完成后才會實例化變量。為避免此錯誤,您可以about.html
使用<div *ngIf="about"> ... </div>
- 3 回答
- 0 關注
- 1595 瀏覽
添加回答
舉報
0/150
提交
取消