簡體   English   中英

如何在Blazor項目中分隔JavaScript文件?

[英]How to separate JavaScript files in a Blazor project?

我有一個超過10個模塊的大項目,每個模塊都有自己的javascript文件,我們一次只能處理一個模塊,不需要全部javascript內容,因此應該全部加載還是分開他們每個模塊? 為每個模塊使用單獨的.cshtml文件托管.razor組件可能是一種解決方案,這是線索嗎?

在.NET Core 3.0的初始發行版中,Blazor本身不支持它。請參閱https://github.com/aspnet/AspNetCore/issues/5465

變體1

如果要動態加載部件中的JS文件,則必須假定Blazor應用程序使用的JS不是Blazor應用程序本身的一部分,而是某個可以加載每個JS模塊的外部環境。 Blazor組件必須自己手動加載JS模塊。

// Include that file in the index.html, but not inside Razor Components Project.
function loadModule(moduleName) {
   // load module somehow, via Ajax probably.
   // This function likely has to make sure that module would not be loaded twice.
}
protected override Task OnInitializedAsync() {
   // Component load JS module.
   await jsRuntime.InvokeVoidAsync("loadModule", "component1");
}

同樣,應該在Blazor項目之外管理JS文件component1.js

版本2

使用Assembly.Load加載組件裝配,並以某種方式手動將組件注入UI。 反思是您的朋友。

// Component1 assumed to be present in the `dist/_framework/_bin` folder where all Blazor stuff is located.
var assembly = Assembly.Load("Component1");
foreach (var type in assembly.GetTypes())
{
    if (!type.FullName.EndsWith("ModuleLoader"))
        return;

    var moduleInstance = Activator.CreateInstance(type);
    // And now all regular .NET plugin stuff.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM