如何解决使用另一个文件的函数,不需要
上下文:
我有 3 个文件,
01/31 14:41:30: Launching 'app' on OnePlus ONEPLUS A6003.
$ adb shell am start -n "com.example.bottles/com.example.bottles.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 31111 on device 'oneplus-oneplus_a6003-2beda810'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
I/example.bottle: [GL_OOM] ClampGrowthLimit 268435456
V/Font: Change font:2
Default family:android.graphics.Typeface@1b512b59
E/Perf: Fail to get file list com.example.bottles
getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
Fail to get file list com.example.bottles
E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/example.bottle: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist,reflection,allowed)
W/example.bottle: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist,allowed)
V/ViewRootImpl: The specified message queue synchronization barrier token has not been posted or has already been removed
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true,DecorView@45d1a26[MainActivity]
I/AdrenoGLES: QUALCOMM build : 35556ba,I9ca166462c
Build Date : 08/07/19
OpenGL ES Shader Compiler Version: EV031.27.02.00
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
Build Config : S P 8.0.8 AArch64
I/AdrenoGLES: PFP: 0x016ee187,ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/: Successfully load libgui-plugin.so,this=0x798f030050
D/OnePlusJankManager: Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.example.bottles/com.example.bottles.MainActivity--- jank level = 1
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bottles,PID: 31111
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setonClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.bottles.RadlerKarte$1.onClick(RadlerKarte.java:34)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I/Process: Sending signal. PID: 31111 SIG: 9
、parent.js
和 child1.js
child2.js
parent.js
let child1 = require("./child1.js")
let child2 = require("./child2.js")
let key = "*****"
child1.start(key)
child2.start();
child1.js
let key = false;
module.exports = {
action: async() => {
return someApi.get(key);
},start: async(_key) => {
key = key;
}
}
child2.js
问题
我需要从 module.exports = {
action: async() => {
let res = await child1.action()
...
},start: async() => {
// startup actions
}
}
内的 child1
运行一个函数,但我不能使用 child2
,因为 require
只能有 1 个实例
有人知道解决办法吗?谢谢
解决方法
我认为你问错了问题:)
如果您需要 child1 是唯一的,您应该使用单音并在您需要的任何地方都需要它。
//child1
let instance
module.export = () => {
if(!instance) {
instance = {
action: async() => {
return someApi.get(key);
},start: async(_key) => {
key = key;
}
}
return instance
}
我个人不喜欢单调方法,但它可以很方便
您可以尝试的另一种方法是将 child1 服务实例注入 child2 'constructor'
你只需要导出一个函数而不是一个对象
parent.js
let child1 = require("./child1.js")()
let child2 = require("./child2.js")(child1)
let key = "*****"
child1.start(key)
child2.start();
child2
module.exports = (child1) => {
const action = async() => {
let res = await child1.action()
...
}
const start = async() => {
// startup actions
}
return {action,start}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。