0
我一直遇到OnInit訪問同一組件類中的函數的問題。Angular2 - OnInit訪問函數
我的基本設置如下:
import {Component, OnInit} from '@angular/core';
...
export class AppComponent implements OnInit {
login(){...}
ngOnInit() {
this.login(); //1
document.onkeypress = function(e){
if (document.body === document.activeElement) {
this.login(); //2
}
};
1將觸發頁面加載登錄功能不如預期,但2抱怨登錄不是一個函數。如何正確訪問AppComponent中的登錄功能?
您需要使用箭頭功能 - 'document.onkeypress =(E)=> {...}' - 保存詞法範圍'這個' – drewmoore