-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSImport: Not enough information regarding interop for non-module code #27554
Comments
Hello @yugabe ... Let's ping @pavelsavara for comment on this. |
Thanks @pavelsavara, I'll take a look. I tried prefixing with "window", but it didn't work (didn't log an error either, but it's probably an issue elsewhere in my code base). This is enough for me to go on. This could maybe worth mentioning in the docs, especially as it seems this is just undocumented, but there are test cases that support it is working. @guardrex? |
I'll take look, but it will probably be tomorrow/Friday due to on-going .NET 7 docs work. |
It could not fail on compile time when the attribute is processed, because we don't know if you provided |
Yes, I was talking about invoking the method, not compilation. Invoking the imported method does nothing currently for me. But as I said, don't worry about it, the problem is most probably elsewhere. If I can reproduce it in a controlled environment, I'll open an issue. |
The issue was indeed in one of my test projects (Blazor didn't register some event handlers correctly, so the generated interop method wasn't called). Using it as @pavelsavara described works as intended. Prefixing with "window" in itself does not work, but "globalThis" does. Thanks for the help. |
The page doesn't mention the
[JSImport]
overload that only takes a singlestring functionName
argument (without thestring moduleName
second parameter):This implies there is an alternate way to reference functions, but the page makes no mention of this. The API/source isn't clear either:
As the alternative
IJSRuntime
-based JS interop takes objects that reside on the global object, it is not straighforward to migrate to the newJSImport
-based solution. This can be increasingly troublesome, because bundling the resulting application code in a JavaScript bundle might not support exporting the bundle as an EcmaScript module (webpack support is only experimental). Bundling (with minification, optimizations etc.) is still considered by many a superior solution than loading individual (small) module files over network and can simplify deployment or caching of resources.The questions unclear are:
[JSImport(string functionName)]
overload of the attribute?I haven't found an easy way to invoke non-module members with
JSImport
, but if it is an intentional design choice, there is a simple hack to work around that:I'd rather the API supported the window/global object by default.
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: