要在Chrome控制台中访问变量,你可以按照以下步骤使用Angular 7和Typescript进行操作:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
exampleVariable: string;
constructor() { }
ngOnInit() {
// 在这里设置变量的值
this.exampleVariable = 'Hello, World!';
// 将变量绑定到全局对象(window),以便在控制台中访问它
(window as any).exampleVariable = this.exampleVariable;
}
}
example.component.html
)中添加组件的内容,例如:{{ exampleVariable }}
app.component.html
中添加以下内容:
启动应用程序并在Chrome浏览器中打开开发者工具的控制台。
在控制台中,你现在可以访问exampleVariable
变量,只需键入exampleVariable
并按回车即可。
注意:确保在控制台中访问变量之前,组件的ngOnInit
生命周期钩子已经执行,以便将变量绑定到全局对象。