简体   繁体   中英

Blazor JS scripts into a blazor component

I am building a Blazor app, I need to dynamically add a JavaScript file that is used only in a single Blazor component. As you would be aware Blazor allows adding script tags only to the root HTML document. This makes it difficult to add JavaScript files that are required only in a single component.

The script is

It is important to set the data-main="payment-js" attribute on the script tag.

Are there any restrictions around iframe rendering in Blazor? The script renders multiple iframes on the specific Blazor components as part a PCI compliant payment system integration.

The script works in a simple HTML file.

I would be grateful for any assistance

This can be achieved by using a script loader and JSRuntime . Check this out.

You could use the special HeadContent component to add the script in the <head> tag from your component.

<HeadContent>
    <script data-main="payment-js" src="gateway.tillpayments.com/js/integrated/…>
</HeadContent>

More info: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/control-head-content?view=aspnetcore-6.0#control-head-content-in-a-razor-component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM