您可以仅在应用程序的一部分中使用Blazor吗?
Blazor被设计为一个平台,您可以在其中创建一个完整的Web应用程序,并且在上次与Blazor进行的实验中我们看到了该平台,我们为我的博客创建了一个独立的搜索网站。但是,就像我们工具箱中的任何工具一样,它并不总是适合您的工作。
以我的博客为例,它几乎是一个只读网站,其内容作为markdown存储在GitHub中,我使用Hugo将其转换为HTML文件。现在确定,可以将它作为Blazor WASM应用程序进行处理,我们可以使用.NET Markdown库,并即时生成页面,但这是运行我的网站并提供给读者带来次佳体验。
但是,如果我们想集成以前构建的搜索应用程序,该如何处理?
了解Blazor的启动方式
为了考虑如何在另一个应用程序中运行Blazor WebAssembly,我们需要学习一些有关Blazor WebAssembly应用程序如何运行的知识。
当您创建一个新项目时,有一个名为 wwwroot/index.html
您可能永远都不会钻研,但这是难题的重要组成部分。看起来像这样:
<元 字符集=“ utf-8” />
<元 名称=“视口” 内容=“宽度=设备宽度” />
项目名
<基础 href =“ https://dev.to/” />
<链接 href =“ css / bootstrap / bootstrap.min.css” rel =“样式表” />
<链接 href =“ css / site.css” rel =“样式表” />
载入中...
实际上,这非常简单,我们需要的重要部分是以下两行:
载入中...
我们将前往
元素,不过首先让我们看一下JavaScript文件。您可能会注意到此文件没有出现在磁盘上的任何位置,这是因为它是构建输出的一部分。您可以在GitHub上的ASP.NET Core存储库中找到此源。 src/Components/Web.JS/src/Boot.WebAssembly.ts
(无论如何在撰写本文时)。该文件与Blazor Server有一些共同点,但主要区别在于使用 MonoPlatform
进行一堆WASM互操作。
该文件非常重要,没有它,您的Blazor应用程序将永远无法启动,因为它负责初始化承载Mono的WASM环境(通过将脚本注入DOM),然后它将使用另一个生成的文件, _framework/blazor.boot.json
,以确定需要将哪些.NET DLL加载到Mono / WASM环境中。
因此,您需要包含此JS文件,并且 _framework
文件夹必须位于根目录,因为这是它查找JSON文件的方式(请参阅此注释)。
延迟加载的外套
在挖矿源代码时遇到的一个有趣的问题是,您可以通过添加来延迟Blazor的负载 autostart="false"
到 tag, as per this line and then call
window.Blazor.start()
in JavaScript to start the Blazor application.
I'm not going to use it for this integration, but it's good to know that you can have a user-initiated initialisation, rather than on page load.
Placing Your Blazor App
Now that we understand what makes our Blazor app start, how do we know where in the DOM it'll appear? Well, that's what the
element in our HTML is for, but how does Blazor know about it?
It turns out that that is something that we control from our 初创公司
class:
using Microsoft.AspNetCore.Components.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace DemoProject
{
public class 初创公司
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IComponentsApplicationBuilder app)
{
app.AddComponent<App>("app");
}
}
}
See how we're using AddComponent
and specifying a DOM selector of app
? That's how it knows what element in the DOM the application will start. This is something that you can change, maybe make it a selector to a ID of a DOM element or to a
, or to anything else that you want, but it's not that important, so I just leave it as
.
Aside: I haven't tried it yet, but given that you specify the DOM element and the entry component (via generics, this points to App.razor
in the above sample) you could potentially have multiple independent Blazor apps running on a page. Why would you do this, I have no idea... but you can in theory!
Hosting Blazor
When it comes to hosting Blazor WASM there are a few options but I want to focus on the Azure Storage static sites approach, which is how my blog is hosted.
First thing we'll need to do is publish the app in Release mode using dotnet publish --configuration Release
. From that we'll grab the contents of the bin/Release/{TARGET FRAMEWORK}/publish/{ASSEMBLY NAME}/dist/_framework
folder, which will contain blazor.boot.json
, blazor.server.js
, blazor.webassembly.js
, a folder called _bin
and a folder called wasm
.
We want to copy this _framework
folder and place it in the root of our static site, maintaining all the paths so that Blazor can start up.
Note: According to the docs you can change the content-root
and path-base
when hosting using dotnet run
but I haven't found them working when it's published. Also, Hugo is very aggressive at setting absolute paths so I found it easiest to put my WASM files in the same structure that dotnet run
used.
Since this is a search application let's create a new page called Search and put in our required HTML:
现在生成您的静态网站(或您正在使用的任何主机)并导航到 /search
路由器。
如果一切正常,您将收到一个错误消息
抱歉,此地址没有任何内容。
剃刀路由
如果您还记得我们的上一篇文章,我们了解到 @page
Razor Components中的指令。在这里,您可以指定页面继续匹配的路线,直到现在为止 @page "https://dev.to/"
那里。但是,我们现在开始 /search
Blazor的路由引擎已经查看了网址并执行了 App.razor
零件:
AppAssembly="@typeof(Program).Assembly">
Context="routeData">
RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
Layout="@typeof(MainLayout)">
Sorry, there's nothing at this address.
自从 Router
找不到匹配的路线来使用 RouteView
反对它陷入 NotFound
这就是为什么我们有此错误
不用担心,这很容易解决,只需更新 @page
指令以匹配您希望其在发布的站点中匹配的路由,或者简化您的 App.razor
不关心路由。
一旦完成了新发布,并且复制的文件就会很高兴。
结论
Blazor是一种构建丰富应用程序的好方法,但是预先生成静态内容并使用Blazor增强应用程序而不是拥有它是有价值的。
在这里,我们看了一些用于在HTML页面中运行Blazor应用程序的重要文件,并且还研究了将其放入其他类型的应用程序所需的操作。
资讯来源:由0x资讯编译自DEV,原文:https://dev.to/dotnet/can-you-use-blazor-for-only-part-of-an-app-3alo ,版权归作者所有,未经许可,不得转载