我在Angular 2中構建應用程序,並且仍然在理解TypeScript的這個範圍時遇到了一些麻煩。在TypeScript中訪問函數內部的類函數
我有一個名爲SharedService打字稿類,當函數的HandleError接收401個狀態,我想它來調用註銷()。這也是班上的一個功能。
一讀,爲了使用功能結合這個正如我在我的例子做了以下我應該使用箭頭函數定義,在某種程度上這仍返回:
TypeError: this.logout is not a function(…)
你們是否知道我做錯了什麼?
export class SharedService {
logout =() => {
console.log('Logout.');
}
//This method catches any errors that might arise upon http requests
handleError(error: any) {
if (error.status === 401) {
this.logout(); <----------------------------- This returns the error
}
console.error(errMsg); // log to console instead
}
}
調用this.logout()時發生錯誤!
當我打電話this.logout(),我編輯我的例子出現的錯誤。 – hY8vVpf3tyR57Xib
這就是我的理解,但我認爲這是你調用'handleError'的方式造成的。你有沒有試過我的建議? –
啊,你說得對。謝謝! – hY8vVpf3tyR57Xib