|
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 friend, I realized something recently: AI was slowing me down. Iād send a prompt⦠and wait. Even though I already knew what to do next. So I changed one thing. I stopped using AI like a tool and started using it like a team. Multiple chats. Multiple tasks. All running at once. Watch it below š Happy prompting! Take care,Patrick P.S. If you want help setting this up for your workflow or your team, click here.
Hey friend, Blazor Server or WebAssembly? Lately, I had a call with a student migrating a desktop app⦠and this question came up immediately. So I made a quick video to break it down: You'll learn: When Blazor Server is the simplest choice When you actually need WebAssembly And when an API becomes necessary Enjoy! Take care,Patrick P.S. Our next live office hours inside the .NET Web Academy start tomorrow. Join here.
Hey friend, Working from home as a .NET developer sounds great. Freedom. No meetings. Full control. But after a while, it gets heavy. You make every decision alone. Architecture. Azure. Refactoring. Deployment. And sometimes you just want someone to say, āYeah, thatās fine. Ship it.ā AI helps. Tutorials help. But real conversations with other developers hit different. In today's video, I talk about why working alone can slow us down and why feedback from other devs matters more than we think....