angular - 在 Angular typescript 中从控制台访问类方法

标签 angular typescript

我需要通过控制台调用类方法,实习生访问类变量,然后它反射(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/

相关文章:

html - 需要从 Angular 垫分页器中删除或自定义分页箭头

javascript - 如何使用 Vue.js 将 TypeScript 文件编译为 JavaScript

node.js - Mongoose 与 NestJS - 一对多

html - 如何在 Angular 的组件文件中设置输入占位符文本

angular - 在 Angular 的 ContentChildren 中渲染组件

angular - 在捕获到故障后,如何允许我的 HTTP 请求继续通过我的应用程序传播?

javascript - Angular2 - md-slide-toggle 显示错误的值

javascript - 属性 'x' 是私有(private)的,只能在类 'y' 内访问

angularjs - 使用自动 ng-bind 创建 ASP MVC 自定义 View

javascript - 如何使用angular material api服务?