【问题标题】:Is there a way to print value in variable in angular4 component from console?有没有办法从控制台打印 angular4 组件中的变量值?
【发布时间】:2018-09-25 21:08:09
【问题描述】:

我使用 Angular 4 将简单键保存在组件中的变量中,当应用程序关闭时,每个值都会被删除,我知道。

这是一个简单的示例:

export class LoginComponent implements OnInit {

  data : any;

  constructor() {
    this.data = "Hello";
  }

}

我只是想知道有没有办法使用浏览器控制台在没有console.log() 的情况下在this.data 中显示价值?

【问题讨论】:

  • console.log() 有什么问题?
  • 你不能从被破坏的组件中创建控制台日志。
  • 我只是想确定,保存在角度组件中的每个值都无法从浏览器控制台访问

标签: html angular angular-components browser-console


【解决方案1】:

是的,你可以。

首先在您的页面中查找一些属于您的组件的 HTML。然后,检查它。

在 Chrome 中,您会在旁边看到一个 $0。这是一个变量引用

现在,进入你的控制台并输入

ng.probe($0).componentInstance

这将记录您的整个组件以及其中的变量。你可以简单地给它一个参考

const myRef = ng.probe($0).componentInstance

然后根据需要删除您的组件,然后直接从控制台再次记录

console.log(myRef) // or shorthand
myRef

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2016-05-31
    • 2010-11-25
    • 2012-03-06
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多