|
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 just released a new video and wanted to share it with you. Watch the video here 👇 I used GitHub Copilot to build a small Blazor weather app. It worked almost instantly and even used real API calls. At first glance, everything looked great. Then I opened the Razor file. All the logic was sitting in one place. UI, API calls, and business logic mixed together. It worked, but it was not something I would ship. In the video, I show how I fix this using a single refactoring prompt. I...
Hey friend, I just released a new video and I think you will enjoy this one. In the video, I ask GitHub Copilot to build a real mortgage app in Blazor. The app works. But the code is messy. All the business logic ends up in the UI. Fast results. Bad structure. Then I clean it up properly. Same behavior. Same output. Much better code using services, interfaces, and dependency injection. It shows why Copilot is powerful, but dangerous without rules. Watch the video here 👇 Enjoy the breakdown...
Hey friend, I just released a new video and I think you’ll enjoy it. In the video, I ask GitHub Copilot to build a .NET Web API using a short, vague prompt. The result works, but the structure feels random and messy. Then I run the same idea again with a stronger prompt that forces a clean structure. Feature folders. Vertical slice. Mediator. Fluent Validation. The difference is night and day. Watch the video here 👇 Enjoy the breakdown and let me know what you think. Take care, Patrick P.S....