简体   繁体   English

Blazor Javascript 隔离与 NPM 依赖项

[英]Blazor Javascript isolation with NPM dependencies

I'm trying to use the new Blazor Javascript isolation feature.我正在尝试使用新的 Blazor Javascript 隔离功能。 I'm importing my own JS file as per the example ExampleJsInterop.cs .我正在按照示例ExampleJsInterop.cs导入我自己的 JS 文件。 It works until I try to import an NPM module from within my script.在我尝试从脚本中导入 NPM 模块之前,它一直有效。 In my package.json I have set up a dependency on interactjs, and in my script I have added import interact from 'interactjs';在我的 package.json 中,我设置了对 interactjs 的依赖,并在我的脚本中添加了import interact from 'interactjs'; at the top.在顶部。

I'm getting a Failed to resolve module specifier "interactjs" error.我收到Failed to resolve module specifier "interactjs"错误。 I'm not sure how to get past that.我不知道如何克服这一点。

Previously I was using Webpack to bundle my script and dependencies together into a single file that is added into my index.html as a tag.以前我使用 Webpack 将我的脚本和依赖项捆绑到一个文件中,该文件作为标签添加到我的 index.html 中。 This was working fine, but I'm not sure how to continue using NPM packages with JS isolation.这工作正常,但我不确定如何继续使用带有 JS 隔离的 NPM 包。

Thanks!谢谢!

A bit late, but I've just finished solving a similar issue.有点晚了,但我刚刚解决了一个类似的问题。 The npm files are installed to the hidden node_modules folder. npm 文件安装到隐藏的 node_modules 文件夹中。 This isn't available to your script when you are running your app, unless you do something to make it available.当您运行应用程序时,这对您的脚本不可用,除非您采取措施使其可用。 however, even if you copied the interactjs file into your scripts folder it would still not work if it was an npm file.但是,即使您将 interactjs 文件复制到脚本文件夹中,如果它是 npm 文件,它仍然无法工作。 Those are meant to run in nodejs not a browser.这些是为了在 nodejs 而不是浏览器中运行。 So you would still need to use your bundler.所以你仍然需要使用你的捆绑器。 I tried webpack, but had some issues with certain files so ended up with snowpack instead.我尝试了 webpack,但某些文件存在一些问题,因此最终使用了 snowpack。 I just finished a bunch of articles on javascript interop - part 4 deals with npm我刚刚完成了一堆关于 javascript 互操作的文章 - 第 4 部分涉及 npm

I forgot that I left this question open for almost a year!我忘了我把这个问题留了将近一年!

I ended up solving it using Snowpack to bundle the NPM package into the Blazor wwwroot folder.我最终使用 Snowpack 将 NPM package 捆绑到 Blazor wwwroot 文件夹中解决了这个问题。 Credit goes to this article for pointing me in the right direction: https://nbarraud.github.io/js-in-blazor.html感谢这篇文章为我指明了正确的方向: https://nbarraud.github.io/js-in-blazor.html

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

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