无法将参数传递给js.Func。

huangapple go评论67阅读模式
英文:

Can not pass parameters to js.Func

问题

我有以下代码,其中函数正确地附加到按钮的onclick事件上:

func f(this js.Value, args []js.Value) interface{} {
    println("hello world")
    return nil
}
btn.Set("onclick", js.FuncOf(f))

我尝试将参数传递给这个函数:

btn.Set("onclick", js.FuncOf(f).Invoke("Hello World"))

并在js.Func中进行操作:

for x := range args {
    println(x)
}

但是我得到了错误:

wasm_exec.js:421 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'exports')
    at syscall/js.valueInvoke (wasm_exec.js:421)
    at syscall_js.valueInvoke (main.wasm:0x133521)
    at syscall_js.Value.Invoke (main.wasm:0x131300)
    at main.html (main.wasm:0x25b572)
    at main.main (main.wasm:0x25e591)
    at runtime.main (main.wasm:0x7f285)
    at wasm_pc_f_loop (main.wasm:0xddf81)
    at wasm_export_run (main.wasm:0xddf54)
    at global.Go.run (wasm_exec.js:577)
    at init (wasm.js:7)

有什么想法吗?

英文:

I've the below code where the function attached properly to the button onclick event:

func f(this js.Value, args []js.Value) interface{} {
	println("hello world")
	return nil
}
btn.Set("onclick", js.FuncOf(f))

I tried to pass args for this function as:

btn.Set("onclick", js.FuncOf(f).Invoke("Hello World"))

And manipulate it in the js.Func as:

	for x := range args {
		println(x)
	}

But I got the error:

wasm_exec.js:421 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'exports')
    at syscall/js.valueInvoke (wasm_exec.js:421)
    at syscall_js.valueInvoke (main.wasm:0x133521)
    at syscall_js.Value.Invoke (main.wasm:0x131300)
    at main.html (main.wasm:0x25b572)
    at main.main (main.wasm:0x25e591)
    at runtime.main (main.wasm:0x7f285)
    at wasm_pc_f_loop (main.wasm:0xddf81)
    at wasm_export_run (main.wasm:0xddf54)
    at global.Go.run (wasm_exec.js:577)
    at init (wasm.js:7)

Any thought?

答案1

得分: 1

我使用.bind解决了这个问题,代码如下所示:

element.Set("onclick", js.FuncOf(f).Call("bind", e, args...)) 
// bind(event, arguments...), 作为(arguments..., event)传递给函数f

所以,我的代码变成了:

btn.Set("onclick", js.FuncOf(f).Call("bind", btn, "hi", "hello"))

func f(this js.Value, args []js.Value) interface{} {
	for index, item := range args {
		if x < len(args)-1 { // len(args)-1 是事件本身
			println(index, item.String())
		}
	}

	self := args[len(args)-1].Get("target")
	// self = event.target == this
	self.Get("style").Call("setProperty", "background-color", "red")
    // 同样的效果:
	// this.Get("style").Call("setProperty", "background-color", "red")
	return nil
}
英文:

I solved it using .bind as below:

element.Set(&quot;onclick&quot;, js.FuncOf(f).Call(&quot;bind&quot;, e, args...)) 
// bind(event, arguments...), passed to function f as (arguments..., event)

So, my code become:

btn.Set(&quot;onclick&quot;, js.FuncOf(f).Call(&quot;bind&quot;, btn, &quot;hi&quot;, &quot;hello&quot;))

func f(this js.Value, args []js.Value) interface{} {
	for index, item := range args {
		if x &lt; len(args)-1 { // len(args)-1 is the event itself
			println(index, item.String())
		}
	}

	self := args[len(args)-1].Get(&quot;target&quot;)
	// self = event.target == this
	self.Get(&quot;style&quot;).Call(&quot;setProperty&quot;, &quot;background-color&quot;, &quot;red&quot;)
    // same as:
	// this.Get(&quot;style&quot;).Call(&quot;setProperty&quot;, &quot;background-color&quot;, &quot;red&quot;)
	return nil
}

huangapple
  • 本文由 发表于 2021年11月17日 22:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70006628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定