将Angular2组件中的变量标记为全局(用于调试)

时间:2016-06-17 21:21:08

标签: typescript angular

将变量设置为全局(临时)对于调试很有用。我们如何将Angular组件中的变量标记为全局变量,以便可以在浏览器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. } 记录变量,但能够与javascript Web控制台中的变量进行交互并使用该变量。

{{1}}

1 个答案:

答案 0 :(得分:2)

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

constructor() {
    this.myvar = "Hello console";

    (window as any).myvar = this.myvar; //DEBUG
}