简体   繁体   English

Blazor<navlink> 作为组件包含时样式停止工作</navlink>

[英]Blazor <NavLink> style stops working when included as component

To reproduce this issue, I created a MenuItemComponent为了重现这个问题,我创建了一个 MenuItemComponent

<li class="nav-item px-3">
    <NavLink class="nav-link" href="fetchdata">
        <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
    </NavLink>
</li>

and then included the component to the NavMenu file然后将该组件包含到 NavMenu 文件中

<div class="top-row pl-4 navbar navbar-dark">
    <a class="navbar-brand" href="">Porcellus</a>
    <button class="navbar-toggler" @onclick="ToggleNavMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <ul class="nav flex-column">
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="counter">
                <span class="oi oi-plus" aria-hidden="true"></span> Counter
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="fetchdata">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
            </NavLink>
        </li>
        <MenuItemComponent></MenuItemComponent>
    </ul>
</div>

@code {
    private bool collapseNavMenu = true;

    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }
}

And then the output is然后 output 是在此处输入图像描述

I don't think this issue exists in asp.net core 3.1我认为 asp.net 核心 3.1 中不存在此问题

It seems that if I wrap contents into a component the css styles will stop working.似乎如果我将内容包装到一个组件中,css styles 将停止工作。

But when I upgraded to .net 5 this issue starts showing destroying the templates I made earlier但是当我升级到 .net 5 时,这个问题开始显示破坏了我之前制作的模板

What is the cause of this and is there a way to fix this?这是什么原因,有没有办法解决这个问题?

Thanks!谢谢!

This is a result of css isolation.这是 css 隔离的结果。 If you want you NavLink component to have the same look you have to create a.css file with the same name as the component with the styles for the navlink in it.如果您希望您的 NavLink 组件具有相同的外观,您必须创建一个与组件名称相同的 .css 文件,其中 styles 用于其中的 navlink。 The styles can be found in the current NavMenu.razor.css. styles 可以在当前的 NavMenu.razor.css 中找到。 Alternately if you want the styles from there to work on sub components you have to use the ::deep combinator .或者,如果您希望 styles 从那里处理子组件,则必须使用::deep 组合器。

MenuItemComponent.razor.css (This was cut & pasted from NavMenu.razor.css ) MenuItemComponent.razor.css 8CBA22E28EB17B5F5C6AE2A266AZ (This was cut & pasted from NavMenu.razor.css )

.oi {
  width: 2rem;
  font-size: 1.1rem;
  vertical-align: text-top;
  top: -2px; }

.nav-item {
  font-size: 0.9rem;
  padding-bottom: 0.5rem; }

.nav-item:first-of-type {
  padding-top: 1rem; }

.nav-item:last-of-type {
  padding-bottom: 1rem; }

.nav-item ::deep a {
  color: #d7d7d7;
  border-radius: 4px;
  height: 3rem;
  display: flex;
  align-items: center;
  line-height: 3rem; }

.nav-item ::deep a.active {
  background-color: rgba(255, 255, 255, 0.25);
  color: white; }

.nav-item ::deep a:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white; }

The alternative solution is to modify NavMenu.razor.css and not use the above css.另一种解决方案是修改NavMenu.razor.css而不使用上面的 css。

.navbar-toggler {
    background-color: rgba(255, 255, 255, 0.1);
}

.top-row {
    height: 3.5rem;
    background-color: rgba(0,0,0,0.4);
}

.navbar-brand {
    font-size: 1.1rem;
}

::deep .oi {
    width: 2rem;
    font-size: 1.1rem;
    vertical-align: text-top;
    top: -2px;
}

::deep .nav-item {
    font-size: 0.9rem;
    padding-bottom: 0.5rem;
}

::deep .nav-item:first-of-type {
    padding-top: 1rem;
}

::deep .nav-item:last-of-type {
    padding-bottom: 1rem;
}

::deep .nav-item a {
    color: #d7d7d7;
    border-radius: 4px;
    height: 3rem;
    display: flex;
    align-items: center;
    line-height: 3rem;
}

::deep .nav-item a.active {
    background-color: rgba(255,255,255,0.25);
    color: white;
}

::deep .nav-item a:hover {
    background-color: rgba(255,255,255,0.1);
    color: white;
}

@media (min-width: 641px) {
    .navbar-toggler {
        display: none;
    }

    .collapse {
        /* Never collapse the sidebar for wide screens */
        display: block;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM