Diese Website verwendet Cookies oder ähnliche Technologien, um Ihr Surferlebnis zu verbessern und personalisierte Empfehlungen bereitzustellen. Indem Sie unsere Website weiterhin nutzen, stimmen Sie unserer Privacy Policy zu: Privacy Policy
Balzor Webassembly call from JS
Short topic description
The goal is to load Blazor Webassembly functionality into JavaSript / TypeScript. In other wods: we want to call a Blzor method from javascript side.
Precondition
✔️ Typescript
✔️ Webpack
✔️ Blazor Webassembly project
The Blazor class
First of all the class
- needs an Attribute which makes it platform specific regarding the usage in a browser and
- the class needs to be partial
as follows
Thats all from C# side...
After building the Blazor project we get a '_framework' folder insinde the 'wwwroot' folder (in my case: net8.0\wwwroot\_framework).
This folder is needed in the webproject.
In our index.html / index.php we need to reference the following script:
<script type='module' src='/js/_framework/blazor.webassembly.js'></script>
(I put the '_framework' folder inside the 'js' folder)
Now we can write another script (I save it in the 'js' folder as worker.js) to call our blazor method. This call will depend on a 'message' event
This event will call the execute message, which will call the C# method and afterwards post data to event subsribers:
On Javascript / Typescript side we will need a worker to subscribe and listen to the message event:
We need a data object which shows whether we got some data from the worker thread (here it is an interface ICallData):
And we need to post our data to the worker thread to trigger the worker an send the data to the wasm library (as method parameter):
The data object callData will be set to true if the worker is ready, which is done within the following function:
Then we wait for it to be ready:
And the wait funcion is defined as follows:
In this function we set a timeout every 1000 ms until callData.called is set to true.
To get this working we need to attach the fctWorker function to the event message listener of the worker:
html import under Typescript / Webpack
Short topic description
Sometimes it is necessary to red some html code within typescript / javascript. To get the file content the import functionality can be used. The following steps will illustrate how to do that.
Precondition
✔️ Typescript
✔️ Webpack
Installation of raw-loader
to load other module types we need to install raw loader:
Global typescript definition
In our project root folder we need to create this file (in case it is not there yet): global.d.ts
Here we add the following code to define the html module import
Add module rule to the webpack.config.js
we add the following code to the module: { rules array:
Define html file
Now we can define the html file with some code e.g.:
Import into typescript
And import it into typescript with
🖋️ html: this is your variable name holding the resulting string
🖋️ ./TheFileName.html: this is the relative path to your file name (int this case it is located in the same folder)