简体   繁体   English

在 blazor 中使用 javascript,直到我点击页面才工作

[英]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.

相关问题 当 Blazor 中的页面更改时,Javascript 不起作用 - Javascript not working when page changes in Blazor Javascript 在使用 Blazor WASM 预渲染时不起作用 - Javascript not working when using Blazor WASM prerendering Javascript/JQuery 直到页面重新加载后才工作 - Javascript/JQuery not working until after page reload Javascript / jQuery元素在重新加载页面之前不起作用 - Javascript/jQuery elements not working until page reload Javascript 单击事件在 Blazor 客户端应用程序中不起作用 - Javascript click event not working in Blazor client side app 使用Blazor(即WebAssembly)+ JavaScript - Using Blazor (i.e. WebAssembly) + javascript javascript-将按钮单击的处理推迟到页面加载后 - javascript - defer processing of a button click until after page loads 我想防止触发第二次点击,直到使用绑定和取消绑定完成动画但无法正常工作为止 - I want to prevent a second click from firing until an animation is complete using bind and unbind but it isnt working 我在没有问题的页面上使用jquery和常规javascript,直到我添加了另一个功能 - I'm using jquery and regular javascript on a page with no probs until I added one more function 如何在初始页面加载时使用CSS隐藏和元素,直到单击Javascript链接 - How can I hide and element using CSS on initial page load until the Javascript link is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM