【问题标题】:Marking variables within Angular2 components as global (for debugging)将 Angular2 组件中的变量标记为全局变量(用于调试)
【发布时间】:2016-06-18 07:11:56
【问题描述】:

将变量设置为全局(临时)对于调试很有用。我们如何将 Angular 组件中的变量标记为全局变量,以便它可以在浏览器 Web 控制台中以交互方式使用? (我不想只使用 console.log 记录变量,而是能够在 javascript Web 控制台中与变量进行交互和使用。

@Component({..})
export class App {
    myvar = null;
    constructor() {
      this.myvar = "Hello World";
    }

      // How do we make myvar global? (temporarily)
      // so that we can use it in the web console to play around with it
      // It's a simple string here but it could be a complex object
      // that we may want to inspect interactively.

}

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    使用 any 绕过 TypeScript 的投诉,并像使用普通 JavaScript 一样附加到 window

    constructor() {
        this.myvar = "Hello console";
    
        (window as any).myvar = this.myvar; //DEBUG
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2017-06-15
      • 2016-10-06
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2023-01-05
      相关资源
      最近更新 更多