最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Blazor: how to include javascript-package, which is installed with Nuget-Package-Manager? - Stack Overflow

programmeradmin2浏览0评论

I'm afraid this will be a stupid question. But I don't manage it to use my JS-Package (for example jQuery), which i have installed with Visual Studio Nuget-Package-Manage in my 5 Blazor Server-App.

What i did: Installing the Package. Here I installed jquery.datatable which includes jQuery itself:

Image of my Project

But now, i don't know how to include it for example in my "_Host.cshmtl"-File:

<script type="text/javascript" charset="utf-8" src="???WHERE IS IT????"></script>

Where is my *.js-File? For example: query.dataTables.js ?? I found it on "C:\Users\xxxxx.nuget\packages\jquery.datatables\1.10.15" and "C:\Users\xxxxxx.nuget\packages\jquery\1.7.0"

Do i realy have to copy it to my wwwroot-Folder manualy? If so, why i should use the package-manager?

Thanks for your help!!

I'm afraid this will be a stupid question. But I don't manage it to use my JS-Package (for example jQuery), which i have installed with Visual Studio Nuget-Package-Manage in my 5 Blazor Server-App.

What i did: Installing the Package. Here I installed jquery.datatable which includes jQuery itself:

Image of my Project

But now, i don't know how to include it for example in my "_Host.cshmtl"-File:

<script type="text/javascript" charset="utf-8" src="???WHERE IS IT????"></script>

Where is my *.js-File? For example: query.dataTables.js ?? I found it on "C:\Users\xxxxx.nuget\packages\jquery.datatables\1.10.15" and "C:\Users\xxxxxx.nuget\packages\jquery\1.7.0"

Do i realy have to copy it to my wwwroot-Folder manualy? If so, why i should use the package-manager?

Thanks for your help!!

Share Improve this question asked Apr 6, 2021 at 11:36 BeemanBeeman 1151 silver badge6 bronze badges 1
  • Package Manager isn't for the client side libraries. You should use the CDN, or Libman. Here's a couple of links. datatables/forums/discussion/56389/datatables-with-blazor and datatables/forums/discussion/60598/… – Nikki9696 Commented Apr 7, 2021 at 17:32
Add a ment  | 

2 Answers 2

Reset to default 4

Traditional web applications using JavaScript normally load the file from a local folder or from a web CDN (e.g. CDNJS. etc). This is then loaded from the page (often referenced from a layout file).

Early on it used to be the case that JS libraries could be loaded via NUGET packages but this approach is now discouraged. It had to fix the creation of the script in a set location, e.g. /Scripts and there was no flexibility. Almost all client-side libraries are now in NPM as packages or on CDNs like cdnjs..

The current approach for .NET web apps to load client-side assets is either use LibMan or NPM and have some sort of webpack arrangement to pile/pack/copy. You would never load the JS from a /packages folder in the way you suggested.

Blazor Approach

Blazor (since .NET 5.0) can load either embedded JS modules (from your code), or from a URL directly.

If you want to package some JS with your application you should look at Razor Component libraries. This allows static assets such as JS files to be embedded in the code, which Blazor makes available via the _content route, e.g. _content/LibraryName/myfile.js.

Because Blazor is a SPA you don't include JavaScript using a <script> tag in your HTML, you should load it as a module and reference it there.

This documentation explains it: https://learn.microsoft./en-us/aspnet/core/blazor/call-javascript-from-dotnet?view=aspnetcore-5.0#blazor-javascript-isolation-and-object-references

DataTables, JQuery

So should you include jquery.min.js and jquery.datatables.min.js in your library? I'd suggest a better approach is to load from a CDN - your package is smaller and there is a chance the URL is already cached and loaded, e.g.

var module = await js.InvokeAsync<IJSObjectReference>(
    "import", "https://cdnjs.cloudflare./ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js");

This loads the module on-demand from the URL directly. You'd also need to load jquery before this.

Finally I'd make this observation: are you sure you want to go down this route?

There are several native Blazor libraries on NUGET for rendering and handling tables. You'll find it much easier to go this way rather than try to patch jquery-based libraries into a Blazor app.

I had a similar issue. Not with the same libraries, but I was wanting to do something that wasn't available in a Blazor library yet. I needed a video player that could handle a certain format that the default HTML 5 video element can't handle. There is an open source player, videoJS , that did the job, but it's a javascript library. It's available on npm and there are cdn's - however the plugins (as far as I could tell) weren't on CDN - so I had to go down the npm route.

When you install an npm package it puts it into a hidden node_modules folder. Unfortunately even if you point to that path or even copy the file in with your other js files it won't work. Npm packages are designed to be run by nodejs, rather than directly in the browser. In order for them to run in a Blazor app (in the browser) you have to do an intermediary step of transpiling it into a browser friendly format.

What I really wanted was a re-usable ponent, that wrapped the javascript. It took me a while to get there but I finally figured it out. I've written a series of articles on my blog detailing it. The final one ports everything into a Razor Class library that can be consumed with no knowledge of the underlying js. The fourth article deals with importing npm libraries and using them within a web assembly app. I'll put the link below but essentially the process is:

  1. Create a folder eg JS and initialise it for npm (npm init -y)
  2. Install the required npm packages (npm install --save)
  3. Create a src folder within the JS folder that that you will put your own js files in
  4. Create an index.js file in src that imports the required javascript modules and exports what you want to consume
  5. Install snowpack (npm install snowpack --save-dev) (or webpack but I found snowpack seems to work better)
  6. Configure snowpack to process the contents of the src folder into wwwroot/js (without snowpack or similar the files in the npm package won't be in a browser or blazor useable format)
  7. use javascript isolation to pick up your index.js file from wwwroot/js

See blog post here for full details (It's part 4 of a 5 part series - part five puts it all in a razor class library so you can add it to a project without ever seeing the javascript)

I know this is late but this SO question was one I kept ing across when searching on how to do what I wanted, so thought I'd put my solution here in case it helps anyone else searching for what I did.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论