angular - 在角度打字稿中从控制台访问类方法
问题描述
我需要通过控制台调用类方法,该控制台实习生访问类变量,然后反映在 HTML 上。我推荐从 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 方法。
您可以通过添加或在以下内容中使整个 angularcomponent.ts
的变量、方法等可在浏览器控制台中访问:OnInit
constructor
假设您正在尝试访问 中的方法AppComponent
,您就去吧。
OnInit() {
window['AppComponent'] = this;
}
或者:
constructor() {
window['AppComponent'] = this;
}
服务也是如此。
然后在控制台中你去:
AppComponent.myMethod()
推荐阅读
- javascript - 如何解构具有多个同名子键的对象
- java - java中有哪些选项可以拆分字符串并删除不需要的字符?
- python-3.x - 是否可以通过api删除创建的电子表格,我的意思是如果我们可以创建电子表格,应该有一个删除它的选项
- shopify - Shopify 脚本标签变量
- regex - 查找两个不同数字并且不允许任何单词字符的正则表达式
- postgresql - 错误:无法扩展文件“pg_tblspc/4815857/PG_11_201809051/16321”:设备上没有剩余空间
- spring-boot - 如何压缩 Spring WebClient 发布请求的正文?
- python - python中'\xe2\x80\x93'和'-'有什么区别?我如何将所有更改为后者?
- wordpress - 基于 cookie 数据的产品价格 - WooCommerce
- python - 为什么我在 RGB 中得到负数?