Comments (2)
Can you also add an example of using JSInterop with a Blazor Component as well?
from blazor.docs.
@danroth27 Having some success and some trouble getting JS interop into the sample. First, I'll explain the two bits that are working, then I'll explain the approach that isn't.
For all examples, the index.html includes:
<script src="exampleJsInterop.js"></script>
Call JS from .NET
<button type="button" onclick="@TriggerJsPrompt">
Trigger JavaScript Prompt
</button>
<h3 id="welcome" style="color:green;font-style:italic"></h3>
@functions {
public async void TriggerJsPrompt()
{
var name = await ExampleJsInterop.Prompt("What's your name?");
await ExampleJsInterop.Display($"Hello {name}! Welcome to Blazor!");
}
}
public static Task<string> Prompt(string text)
{
// showPrompt is implemented in wwwroot/exampleJsInterop.js
return JSRuntime.Current.InvokeAsync<string>(
"exampleJsFunctions.showPrompt",
text);
}
public static Task<string> Display(string welcomeMessage)
{
// displayWelcome is implemented in wwwroot/exampleJsInterop.js
return JSRuntime.Current.InvokeAsync<string>(
"exampleJsFunctions.displayWelcome",
welcomeMessage);
}
window.exampleJsFunctions = {
showPrompt: function (text) {
return prompt(text, 'Type your name here');
},
displayWelcome: function (welcomeMessage) {
document.getElementById('welcome').innerText = welcomeMessage;
}
}
That works.
Call .NET from JS
Static approach
<button type="button" onclick="exampleJsFunctions.returnArrayAsyncJs()">
Trigger .NET static method ReturnArrayAsync
</button>
@functions {
[JSInvokable]
public static Task<int[]> ReturnArrayAsync()
{
return Task.FromResult(new int[] { 1, 2, 3 });
}
}
window.exampleJsFunctions = {
returnArrayAsyncJs: function () {
DotNet.invokeMethodAsync('BlazorSample', 'ReturnArrayAsync').then(data => {
data.push(4);
console.log(data);
})
}
};
That works.
Instance approach
This is where things break down. 💥 🚑
I don't understand from the content in the topic how it's supposed to work.
In the topic example, where is "Blazor" supplied for the output "Hello, Blazor!"?
https://blazor.net/docs/javascript-interop.html#invoke-net-methods-from-javascript-functions
For the example in the sample app, what I'd like to do is have the component call the JS directly from a button (onclick="exampleJsFunctions.sayHello('Blazor')"
). Then, the JS creates the .NET class HelloHelper
and ultimately calls the HelloHelper.SayHello
method ... perhaps that method just shoots the "Hello, Blazor!" to the console.
from blazor.docs.
Related Issues (20)
- Translate documentation to [pt-br] HOT 2
- Update JS interop to document appropriate use of IJSRuntime with server-side Blazor HOT 1
- Add new sample (Blazor Paint)
- 404 on Get Started :( HOT 1
- Request for new entry in Book section HOT 6
- Add the TabSet example in sample app
- Update Blazor hosting models topic for 0.7.0
- Update Host and deploy for 0.7.0 HOT 1
- Component input validation HOT 5
- Add the doc for bind-property-event syntax HOT 4
- Explain the difference between for and foreach loop in context of lambda expression HOT 4
- Add new sample (Blazor Excel) HOT 11
- Add new sample and library (WebSocket)
- Add section about what is and how to create dynamic components HOT 4
- Broken API link
- Getting started with Blazor - documentation incorrect HOT 3
- Razor Components + EF Core HOT 1
- Redirects
- "What is Blazor" link is broken
- Redirection to newer getting started tutorial HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blazor.docs.