How do i use Blazor components in the code?

user3713080

I'm trying to accomplish something like the code below but I can't seem to figure out how to do it.

@page "/"

<div>@test</div>
@code {
[Parameter]
public string Animal { get; set; }

private BaseComponent test { get; set; }

protected override async Task OnInitializedAsync()
{
    switch (Animal)
    {
        case "Cat": test = <Cat>Fluffy</Cat>; break;
        case "Dog": test = <Dog>Woff</Dog>; break;
        default: test = <p>None</p>
    }
}

I also want to be able to put components in a dictionary like new Dictionary<string, BaseComponent> { {"cat", <Cat>test</Cat> }}

any ideas?

Henk Holterman

It is possible with RenderFragments:

private Dictionary<string, RenderFragment> switcher 
  = new Dictionary<string, RenderFragment>(StringComparer.CurrentCultureIgnoreCase)
{
    {  "cat" , __builder => { <Cat> It's a Cat </Cat> } },
    {  "dog" , __builder => { <Dog> It's a Dog </Dog> } },

};

and then outside of @code,

@switcher["Cat"]

But if this is the best solution depends very much on the exact use-case, and how much flexibility is desired in using the <Animal> tags.

You could for instance replace the above with a <ShowAnimal Animal="Cat" /> component and implement that with a (big) switch statement in the markup section.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I properly use a Callback in this code? (Kivy)

분류에서Dev

How do I use MapStateListener?

분류에서Dev

How do I do collapsed="false" in this code

분류에서Dev

How do I use "EOL" and bash redirects to pip a snippet of code to a file using "cat"?

분류에서Dev

how do i make my code shorter

분류에서Dev

How do I look at the source code for a command?

분류에서Dev

How do I code for an insert with a foreign key?

분류에서Dev

How do I use OR with if statements in regards to strings

분류에서Dev

Python: How do i use itertools?

분류에서Dev

How do I use runit with zookeeper

분류에서Dev

How do I use Firefox cookies with Wget?

분류에서Dev

How do I use the GeometryConstraint class?

분류에서Dev

How do I make use of rotation matrices?

분류에서Dev

How do I configure ActiveJob to use Resque?

분류에서Dev

How do i use SetPixel on a new Bitmap?

분류에서Dev

How do i get cURL to use https

분류에서Dev

How do I use "<<" with my own struct?

분류에서Dev

How do I use URL directories as variables?

분류에서Dev

What is fileAPI ? How do I use it with angular?

분류에서Dev

How do I open and use phpmyadmin?

분류에서Dev

How do I configure Docker to use ZFS?

분류에서Dev

How do I use the latest GCC on Ubuntu?

분류에서Dev

How do I use man pages to learn how to use commands?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

how can i use function in $message statement in the following code

분류에서Dev

How can I "install" fonts to use in Visual Studio Code?

분류에서Dev

How should I use Angular services with factories for code reuse?

분류에서Dev

How do I output a button and a text when I hover on an image? Do I need to use JQuery?

Related 관련 기사

뜨겁다태그

보관