简体   繁体   English

如何在Blazor服务器razor文件中加载一个JS库?

[英]How to load a JS library in Blazor server razor file?

I'm trying to follow the document HERE to load THIS bootstrap-datepicker within a blazor server app.我正在尝试按照此处的文档在 blazor 服务器应用程序中加载bootstrap-datepicker。 Note: I want to load it for a single page only without having to load it for all pages.注意:我只想为单个页面加载它,而不必为所有页面加载它。

Within the.razor file I'm trying to do this:在 .razor 文件中,我正在尝试这样做:

HTML HTML

<input @ref=ReferenceToInputControl class="form-control" id="obdInput">

Code代码

@code {
    ElementReference ReferenceToInputControl;
    private IJSObjectReference? jsModule;
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            jsModule = await JS.InvokeAsync<IJSObjectReference>("import", "./js/bootstrap-datepicker.js");
            await jsModule.InvokeVoidAsync("Datepicker", ReferenceToInputControl);
        }
    }

}

I get an error that "Could not find Datepicker. Datepicker was undefined".我收到一条错误消息“找不到 Datepicker。Datepicker 未定义”。

I've read so much information about IIFE and scope and closure and modules but I can't for the life of me figure out what I need to do with that function so it will load and I can use it to change the input into a calendar.我已经阅读了很多关于 IIFE 和 scope 以及闭包和模块的信息,但我无法终生弄清楚我需要用那个 function 做什么,所以它会加载,我可以用它来将输入更改为日历。

That library isn't using ES Modules, so import won't work.该库未使用 ES 模块,因此import无效。 The way it's distributed, it's designed to be loaded in a script tag:它的分发方式被设计为在脚本标签中加载:

<script type="text/javascript" src="js/bootstrap-datepicker.min.js"></script>

You'll also need the CSS from the same repo, as well as jQuery and Bootstrap.您还需要来自同一存储库的 CSS,以及 jQuery 和 Bootstrap。

However it's worth noting that that library is fairly old, does not have Bootstrap 5 support, and will need some adjustments to use with Bootstrap 3 or 4 see here .但值得注意的是,该库相当陈旧,不支持 Bootstrap 5,需要进行一些调整才能与 Bootstrap 3 或 4 一起使用, 请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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