简体   繁体   English

从 angular typescript 中的控制台访问 class 方法

[英]Accessing class method from console in angular typescript

I need to call the class method through console which intern accesses the class variable and then it reflects on the HTML.我需要通过控制台调用 class 方法,该控制台实习生访问 class 变量,然后它反映在 HTML 上。 I Referred Call a service function from browser console in angular 6 but I was unable to access this keyword.我推荐从 angular 6 的浏览器控制台调用服务 function但我无法访问此关键字。

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.
    }

I assume that you are trying to access a typescript method on the browsers console.我假设您正在尝试在浏览器控制台上访问 typescript 方法。

You can make the entire angular component.ts 's variables, methods etc. available to access in the browser console by adding OnInit or in the constructor the following:您可以通过添加OnInit或在constructor中使整个 angular component.ts的变量、方法等可在浏览器控制台中访问:

Let's say you are trying to access a method within the AppComponent , you go.假设您正在尝试访问AppComponent中的方法,您是 go。

OnInit() {
    window['AppComponent'] = this;
}

Or:或者:

constructor() {
    window['AppComponent'] = this;
}

The same works in services too.服务也是如此。

Then in the console you go: AppComponent.myMethod()然后在控制台中你 go: AppComponent.myMethod()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM