我正在构建一个用于 Blazor 客户端应用程序的 razor 组件。我想知道如何使组件中使用的图像动态化,并将图像设置在我调用组件的页面代码块中。
这是我的 Carousel.razor 组件
@inject IJSRuntime JsRuntime
@namespace BlazorFrontend.Components
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1" ></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2" ></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="pictures/foo.jpg" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="pictures/bar.jpg" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="pictures/foobar.jpg" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" role="button" id="carouselExampleIndicators-prev" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" id="carouselExampleIndicators-next" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
@code{
protected override async Task OnInitializedAsync()
{
foreach (System.Collections.DictionaryEntry de in Environment.GetEnvironmentVariables())
{
System.Console.WriteLine("ENVVAR: key=" + de.Key + " value=" + de.Value);
System.Console.WriteLine(Environment.GetEnvironmentVariable("ASSETS"));
}
}
private string AssetsEndpoint = Environment.GetEnvironmentVariable("ASSETS");
bool firstRender = true;
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JsRuntime.InvokeAsync<object>("initializeCarousel");
firstRender = false;
}
}
}
理想情况下,我想做这样的事情:
<img class="d-block w-100" src=@slide1 alt="First slide">
code{
string slide1;
}
在页面的代码块中,我正在使用以下组件:
<Carousel></Carousel>
code{
slide1 = "pictures/foo.jpg"
}
例如,但当然,这是行不通的。这样做的正确/Blazor 方式是什么?