[英]using javascript in blazor, not working until i click page
I have ran into an issue, as i'm loading my javascript through the interop onAfterRenderAsync
method, I can't get my javascript to load (on page load).. The javascript only works once I have clicked onto the page.. I know why this is, obviously because my javascript is waiting for a onclick function...我遇到了一个问题,因为我正在通过互操作
onAfterRenderAsync
方法加载我的 javascript,我无法让我的 javascript 加载(在页面加载时)。ZDE9B9ED78D7E2E1DCEEFFEE780E2F91.9Z 仅在点击页面后才有效知道这是为什么,显然是因为我的 javascript 正在等待 onclick function...
But I want to avoid this and have the javascript load without myself having to attach an onclick function.但是我想避免这种情况,并且无需自己附加 onclick function 就可以加载 javascript。
Can anyone help?任何人都可以帮忙吗?
So basically, I have a navbar with a burger expander ( to show or hide my sidebar ), but this only works, once I have clicked onto the screen.所以基本上,我有一个带有汉堡扩展器的导航栏(显示或隐藏我的侧边栏),但这只有在我点击屏幕后才有效。
I want it to work straight away...我希望它立即工作...
Here is the javascript:这是 javascript:
function LoadContent() {
function LoadContent() {
document.addEventListener("DOMContentLoaded", document.onclick = function(event) {
const showNavbar = (toggleId, navId, pId, headerId) => {
const toggle = document.getElementById(toggleId),
nav = document.getElementById(navId),
bodypd = document.getElementById(pId),
headerpd = document.getElementById(headerId);
mainbodypd = document.getElementById("mainbody");
if (toggle && nav && bodypd && headerpd && mainbodypd) {
toggle.addEventListener("click", () => {
nav.classList.toggle("show");
toggle.classList.toggle("bx-x");
bodypd.classList.toggle("body-pd");
headerpd.classList.toggle("body-pd");
mainbodypd.classList.toggle("height-100short");
});
}
};
showNavbar("header-toggle", "nav-bar", "body-pd", "header");
const linkColor = document.querySelectorAll(".nav_link");
function colorLink() {
if (linkColor) {
linkColor.forEach(l => l.classList.remove("active"));
this.classList.add("active");
}
}
linkColor.forEach(l => l.addEventListener("click", colorLink));
});
}
}
And Blazor:和 Blazor:
protected override async Task OnAfterRenderAsync(bool firstRender){
if (firstRender){
await JS.InvokeVoidAsync("LoadContent");
await JS.InvokeVoidAsync("ToolTipInit");
StateHasChanged();
}
}
You can try this method.你可以试试这个方法。 Create a JavaScript file in your root folder or anywhere in wwwwroot folder, for example "MyCode.js".
在根文件夹或 wwwwroot 文件夹中的任何位置创建 JavaScript 文件,例如“MyCode.js”。 Copy your JavaScript function in there but add export at the beginning.
在那里复制您的 JavaScript function 但在开头添加导出。 It should be something like this:
它应该是这样的:
export function LoadContent() {
Compeletly remove your document.addEventListener("DOMContentLoaded, document.onclick
part from your code and save the file.从您的代码中完全删除您的
document.addEventListener("DOMContentLoaded, document.onclick
部分并保存文件。
Inject IJSRuntime to your Blazor component like this:将 IJSRuntime 注入到您的 Blazor 组件中,如下所示:
@inject IJSRuntime JSRuntime
In your Code section of your Blazor component add this code:在 Blazor 组件的代码部分添加以下代码:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await using var module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./MyCode.js");
await module.InvokeVoidAsync("LoadContent");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.