javascript - 无法从 Chrome 控制台看到变量,但可以在代码中访问

标签 javascript typescript google-chrome-devtools

为什么变量可以从方法内部访问,但不能从浏览器控制台访问?

我有以下 typescript 代码,使用 angular6,通过 ng serve 在 Windows 10,当前的 Chrome 上运行。

import * as d3 from "d3"; // from "npm i d3 --save" version 5.7
// picking a member of d3, nothing magical about "scaleLinear" here
console.log("see", d3.scaleLinear) // it prints ok, ƒ linear() {...

// angular component code fragment below

somefunction() {
    console.log("see again", d3.scaleLinear) // it prints ok, again
    // add a break here
}

我将代码驱动到那个函数。休息时,当我在 Chrome 控制台中尝试时:

> console.log(d3.scaleLinear)

它抛出一个错误:

VM2012:1 Uncaught ReferenceError: d3 is not defined
    at eval (eval at push../src/app/user-sel/user-sel.component.ts.UserSelComponent._resyncYSel (user-sel.component.ts:58), <anonymous>:1:13)
    at UserSelComponent.push../src/app/user-sel/user-sel.component.ts.UserSelComponent._resyncYSel (user-sel.component.ts:58)
    at UserSelComponent.push../src/app/user-sel/user-sel.component.ts.UserSelComponent.selYSel (user-sel.component.ts:166)
    at Object.eval [as handleEvent] (UserSelComponent.html:75)
    at handleEvent (core.js:21673)
    at callWithDebugContext (core.js:22767)
    at Object.debugHandleEvent [as handleEvent] (core.js:22470)
    at dispatchEvent (core.js:19122)
    at core.js:19569
    at HTMLSpanElement.<anonymous> (platform-browser.js:993)

如何在控制台中直接访问 d3?我错过了什么?我查看了有关此主题的旧 SO 问题 52332640却找不到确凿的答案。

最佳答案

如果您的页面有任何框架,则您的控制台可能在错误的上下文中运行。 有一个下拉菜单可让您指定正确的上下文

例如,在此屏幕截图中,有两个 IFrame 元素。为了从“contentIframe2”中运行的脚本访问变量,我必须切换上下文

Console Context

关于javascript - 无法从 Chrome 控制台看到变量,但可以在代码中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879089/

相关文章:

javascript - 弹出窗口自动关闭

javascript - 在 Chrome Devtools 中检测和观察对样式表所做的更改

javascript - 清除主干模型/集合内存泄漏

google-chrome - Chrome iOS(和 Safari)的远程调试

javascript - for 循环中的 jQuery 延迟队列

javascript - 使用 8th Wall SDK 与多个图像目标进行增强

javascript - JS嵌套对象访问全局变量

typescript - 无法使用比较器自定义排序我的 ag-grid

typescript - 如何修复 `declared but its value is never read` 误报?

jquery - typescript 编译器错误。当前范围内不存在名称 'boolean'