在React Native中记录当前目录/文件名

Dan*_*Dan 5 react-native

我想知道是否有办法在React Native中记录当前文件名或目录.类似于NodeJS如何使用__filename__dirname.

atl*_*teh 5

不,这是不可能的,因为所有源代码都捆绑在一起到一个大文件中,然后提供源映射,以便您可以在原始结构中对其进行调试。
您可以通过在代码中的某个位置调用此代码片段来看到这一点:

console.log(new Error().stack);
Run Code Online (Sandbox Code Playgroud)

在常规的 javascript 中,您将获得包含文件和行号的完整跟踪,但在 React-native 中,您将获得类似以下内容的内容:

Error
at Login (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:83009:9)
at instantiate (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:5712:18)
at new Login (eval at proxyClass (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:5730:16), <anonymous>:4:17)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22051:19)
at ReactCompositeComponentWrapper._constructComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22033:13)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:21952:15)
at Object.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:20425:29)
at ReactCompositeComponentWrapper.performInitialMount (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22134:28)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:22015:13)
at Object.mountComponent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:20425:29)"
Run Code Online (Sandbox Code Playgroud)

这表明真正的文件已经消失了。

  • 如果我通过 chrome 运行带有远程调试的react-native。我确实在每个 console.log 的最右侧获得了文件号和行号。有没有办法可以在我的代码中访问该信息?即使只是在调试时。 (7认同)