首页 文章

mat-menu的样式不使用角度材料和flex css在角度工作

提问于
浏览
2

我正在尝试使用角度材料和flex css在角度2中创建一个简单的布局 .

以下是我的代码......

<div  class="background" >

<mat-toolbar fxLayout="row"   fxLayoutAlign="space-between  none" >

<div  fxLayout="column" > 
<p>SPACE STUDY</p>
<h6>Rockefeller FY 2018</h6>

<div  fxShow="true" fxHide.gt-sm="true" fxLayout="column" fxLayoutAlign="start none">

 <h6 [matMenuTriggerFor]="dashboarditems">
  <mat-icon style="transform: scale(1.2);">line_weight</mat-icon> </h6>

  <mat-menu  class="mat-menu-panel"  [overlapTrigger]="true" #dashboarditems="matMenu">

<a routerLink='/home' routerLinkActive='active'> <span matTooltip="HOME"><mat-icon style="color:lightgreen;">home</mat-icon></span></a>


<a > <span matTooltip="SPACE SURVEY"><mat-icon style="color:deeppink;">explore</mat-icon></span></a>


<a > <span matTooltip="SPACE ADMIN"><mat-icon style="color:lightblue;">account_circle</mat-icon></span></a>


<a > <span matTooltip="REPORTS"><mat-icon style="color: orange;">assignment</mat-icon></span></a>


<a > <span matTooltip="JOINT USE"><mat-icon style="color:yellow;">supervisor_account</mat-icon></span></a>


<a > <span matTooltip="HELP"><mat-icon style="color:red;">help</mat-icon></span></a>


</mat-menu> </div> </div> <div fxLayout="row" fxShow="true" fxHide.lt-md="true" > <button mat-raised-button routerLink='/home' routerLinkActive='active'> <span matTooltip="HOME"><mat-icon style="color:lightgreen;">home</mat-icon></span></button> <button mat-raised-button > <span matTooltip="SPACE SURVEY"><mat-icon style="color:deeppink;">explore</mat-icon></span></button> <button mat-raised-button > <span matTooltip="SPACE ADMIN"><mat-icon style="color:lightblue;">account_circle</mat-icon></span></button> <button mat-raised-button > <span matTooltip="REPORTS"><mat-icon style="color: orange;">assignment</mat-icon></span></button> <button mat-raised-button > <span matTooltip="JOINT USE"><mat-icon style="color:yellow;">supervisor_account</mat-icon></span></button> <button mat-raised-button > <span matTooltip="HELP"><mat-icon style="color:red;">help</mat-icon></span></button> </div> </mat-toolbar> </div> <style> .background { /* Remember to use the other versions for IE 10 and older browsers! */ display: flex; min-height: 100%; font-family: 'verdana', sans-serif; color: #fff; height:100vh; background: #222222; background: #16222A; /* fallback for old browsers */ background: -webkit-linear-gradient(to top, #16222A , #3A6073); /* Chrome 10-25, Safari 5.1-6 */ background: linear-gradient(to top, #16222A , #3A6073); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ } p{ font-size:20px; font-weight:bold; font-family: 'verdana', sans-serif; margin:3px; } h6{ font-size:12px; font-weight:bold; font-family: 'verdana', sans-serif; } .mat-raised-button{ text-align:left; border-radius:10px; font-size:11px; font-weight:bold; font-family: 'verdana', sans-serif; color:white; background:transparent; } .mat-icon { transform: scale(1.5); } .matTooltip{ font-size:11px; font-weight:bold; font-family: 'verdana', sans-serif; } .mat-menu-panel { min-width: 35px; max-width: 280px; overflow: auto; -webkit-overflow-scrolling: touch; max-height: calc(100vh - 48px); border-radius: 2px; outline: 0; background: transparent; padding-left: 10px; } </style>

这是我的输出

enter image description here

在这里,我试图将mat-menu容器的背景颜色更改为透明,并设置其他一些样式 .

当我更改控制台时,它会被反射回来,如下所示

enter image description here

但是,当我在代码中应用相同的更改时,样式未应用 .

我还尝试在组件和我添加mat菜单的div中明确添加class =“mat-menu-panel” .

但仍然没有应用样式..

任何人都可以帮我修理这个样式..

1 回答

  • 2

    由于Angular view encapsulation,因此未应用该样式 .

    你可以在这里看到你的 .mat-menu-panel 得到 encapsulated

    mat-menu-panel after view encapsulation

    解决方案 - 三种选择:

    • turn off the view encapsulation:

    encapsulation 组件属性设置为 ViewEncapsulation.None ,如下所示:

    @Component({
            selector: 'app-root',
            templateUrl: './app.component.html',
            encapsulation: ViewEncapsulation.None, // disable ViewEncapsulation
        })
    

    注意:

    您的某些属性会被默认样式覆盖,因此您必须为每个属性添加 !importantuse more specific selector .

    例如,我在您的 mat-menu 中添加了 myMenu 类:

    .myMenu class

    然后,将css选择器更新为:

    .mat-menu-panel.myMenu {
        min-width: 35px;
        max-width: 280px;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
        max-height: calc(100vh - 48px);
        border-radius: 2px;
        outline: 0;
        background: transparent;
        padding-left: 10px !important;
    }
    

相关问题