简体   繁体   English

如何从 Blazor 调用 JavaScript 类构造函数

[英]How to call a JavaScript class constructor from Blazor

If I have JavaScript classes defined in a library like three.js, how can I call the constructor from Blazor WebAssembly and instantiate objects?如果我在像 three.js 这样的库中定义了 JavaScript 类,我如何从 Blazor WebAssembly 调用构造函数并实例化对象?

I have tried我努力了

    var threeJs = await Js.InvokeAsync<IJSInProcessObjectReference>("import", "https://threejsfundamentals.org/threejs/resources/threejs/r125/build/three.module.js");
    var scn = threeJs.Invoke<IJSInProcessObjectReference>("new THREE.Scene");

but this results in an error saying " Microsoft.JSInterop.JSException: Could not find 'new THREE.Scene' ('new THREE' was undefined). "但这会导致错误提示“ Microsoft.JSInterop.JSException: Could not find 'new THREE.Scene' ('new THREE' was undefined).

Is it possible to instantiate JavaScript classes without making a JavaScript wrapper function which instantiates the object?是否可以在不创建实例化对象的 JavaScript 包装函数的情况下实例化 JavaScript 类?

You can't call new THREE.Scene by itself.您不能单独调用new THREE.Scene You can do something like const scene = new THREE.Scene .您可以执行类似const scene = new THREE.Scene But, I don't know what good instantiating a variable in JSInterop would do.但是,我不知道在 JSInterop 中实例化一个变量会有什么好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM