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