我需要通过控制台调用类方法,实习生访问类变量,然后它反射(reflect)在 HTML 上。
我推荐了 Call a service function from browser console in angular 6但我无法访问此关键字。
export class AppComponent {
title = 'calender-app';
timeslot: any = [];
constructor(){
window['plotEvent'] = this.plotEvent;
}
plotEvent(events: Calender[]): void{
// not able to access this.timeslot when I call it from browser's console.
this.timeslot.push(...events); // getting error as can't read property timeslot of undefined.
}
最佳答案
我假设您正在尝试访问浏览器控制台上的 typescript 方法。
您可以使整个 Angular component.ts
的变量、方法等可通过添加 OnInit
在浏览器控制台中访问或在 constructor
下列:
假设您正在尝试访问 AppComponent
中的方法。 , 你走。
OnInit() {
window['AppComponent'] = this;
}
或者:constructor() {
window['AppComponent'] = this;
}
这同样适用于服务。然后在控制台中你去:
AppComponent.myMethod()
关于angular - 在 Angular typescript 中从控制台访问类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63008382/