Hey Reader, Today, I show you how combining Blazor and JavaScript can give you the best of both worlds. Letās dive into how you can easily mix the power of these two technologies in your projects! Watch on YouTube šŗCheck out the full tutorial on YouTube to see everything in action š A Simple Example: Console Logging in BlazorImagine youāre working on a Blazor Server application, and you want to log the current count (of the Counter page) to the console. Your first thought might be to use
This works, but youāll only see the output in the terminal, not the browser console. But what if you want to log it directly in the browser console? This is where JavaScript comes in handy. Injecting JavaScript into BlazorTo use JavaScript, you can inject the
When you run this, youāll see the log directly in your browserās console. This is a basic example, but you can use this approach to call any JavaScript function from Blazor. Using Custom JavaScript FilesBut what if you want to use your own JavaScript functions? Just create a new JavaScript file in your projectās
Now, reference this file in your Finally, call this custom function from Blazor:
When you click the button, youāll see your custom log message in the console. Logging Complex ObjectsWant to log an entire object instead of just a text or value? You can easily do this with JavaScript too. For example, letās say you have a
You can log this object directly:
JavaScript will display the object in the console, showing all its properties. Calling Blazor from JavaScriptSometimes, you might want to call a Blazor function from JavaScript. Although itās less common, itās possible! Hereās a quick example: Create a static Blazor method and mark it with the
In your JavaScript, call this method:
When this JavaScript function runs, it calls the Blazor method and increments the counter. Wrapping UpCombining Blazor with JavaScript opens up a world of possibilities for your web development projects. Whether youāre logging data, handling complex objects, or calling Blazor methods from JavaScript, these techniques can make your applications more powerful and flexible. What's your experience with Blazor and JavaScript? Any real-world examples where you need a specific JavaScript library? Happy coding and have an awesome weekend! Take care, Patrick PS: Need help understanding .NET & Blazor? There are two ways I can help you with:
PPS: Would you like to sponsor this newsletter? Click here. š |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, If you're building apps with Blazor, you've probably asked yourself this: š Should I use SSR, Blazor Server, or WebAssembly? Itās a common question - and I get it a lot. So I made a brand-new YouTube video walking you through how to choose the best one for your app. š„ Watch it here: If you're building a Blazor app or planning to do so soon, this should help clear up a lot of confusion. Let me know what you think, and feel free to reply if you have any questions. Take care, Patrick...
Hey Reader, You may have seen the recent blog post from Jimmy Bogard (creator of MediatR and AutoMapper) announcing that these libraries are going commercial. And to be clear, I fully support that. Time is money, and supporting your family always comes first. BUT⦠In most of my projects, I donāt actually use everything MediatR offers. I usually just need a small part of it, the classic "send a request, get a response" pattern. So I thought: why not build a simple version myself? š Thatās...
Hey Reader, In my last video, I showed you how to build a CRUD app using Vertical Slice Architecture and controllers in .NET 9. But⦠What if you could do the same thing - cleaner, simpler, and without all that controller clutter? š Thatās exactly what I show you in my brand-new video: In this one, youāll learn how to: Use Minimal APIs instead of controllers Keep your Program.cs neat and tidy Stay fully organized with the Vertical Slice Architecture Work with Carter, a super helpful library...