admin管理员组

文章数量:1392068

I'm wondering if there is a way in a Blazor app to load a stylesheet using a conditional of some sort? I want to load different css according to a variable or user setting. Less about where the setting will come from, more about how to actually do this in app.razor

Such as:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />   
    *** some conditional if here:
        <link rel="stylesheet" href="condition1.min.css" />
    *** else this
        <link rel="stylesheet" href="condition2.min.css" />

    < rest of links...>
    <HeadOutlet />
</head>

<body>
    <Routes />
    blah blah
</body>

</html>

TYIA

I'm wondering if there is a way in a Blazor app to load a stylesheet using a conditional of some sort? I want to load different css according to a variable or user setting. Less about where the setting will come from, more about how to actually do this in app.razor

Such as:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />   
    *** some conditional if here:
        <link rel="stylesheet" href="condition1.min.css" />
    *** else this
        <link rel="stylesheet" href="condition2.min.css" />

    < rest of links...>
    <HeadOutlet />
</head>

<body>
    <Routes />
    blah blah
</body>

</html>

TYIA

Share Improve this question edited Mar 13 at 1:19 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 11 at 19:09 MX313MX313 1651 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can treat app.razor just like any other razor component.

So you could just write

@if (SomeProperty)
{
   <link rel="stylesheet" href="condition1.min.css" />
}
else
{
   <link rel="stylesheet" href="condition2.min.css" />
}

and add the property below in a @code segment:

@code
{
    public bool SomeProperty { get; set; } 

    //...
}

本文标签: