【问题标题】:Style for mat-menu not working in angular using angular material and flex cssmat-menu的样式不能使用角度材料和flex css以角度工作
【发布时间】:2018-10-06 00:08:12
【问题描述】:

我正在尝试使用 angular material 和 flex css 在 angular 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"><br/><br/>
<a  routerLink='/home' routerLinkActive='active'>
<span matTooltip="HOME"><mat-icon style="color:lightgreen;">home</mat-icon></span></a><br/><br/><br/>

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

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

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

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

<a  >
<span matTooltip="HELP"><mat-icon style="color:red;">help</mat-icon></span></a><br/><br/><br/>
  </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>

这是我的输出

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

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

但是当我在我的代码中应用相同的更改时,样式并没有被应用。

我还尝试在组件和我添加 mat-menu 的 div 中显式添加 class="mat-menu-panel"。

但仍然没有应用样式..

谁能帮我修复这个样式..

【问题讨论】:

  • 尝试在你的 CSS 属性末尾添加 !important
  • 只是!重要..?
  • 试一试或者给出示例代码。这样便于调查
  • stackblitz.com/edit/…... 你能访问这个 stackblitz 演示吗
  • 你的堆栈闪电战坏了。请修复它。

标签: angular drop-down-menu angular-material angular-flex-layout


【解决方案1】:

由于 Angular view encapsulation,样式未应用。

您可以在此处看到您的 .mat-menu-panel封装

解决方案 - 三个选项:

  • 关闭视图封装:

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

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

注意:

您的某些属性将被默认样式覆盖,因此您必须为每个属性添加 !important使用更具体的选择器

例如,我将myMenu 类添加到您的mat-menu

然后,将 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;
}

【讨论】:

  • 设置encapsulation: ViewEncapsulation.None 并设置组件根的CSS mat-menu 被使用。为避免影响其他mat-menu,为每个菜单提供不同的 CSS 类名称。非常感谢@陪审团
猜你喜欢
  • 1970-01-01
  • 2021-09-01
  • 2020-08-07
  • 2021-01-02
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多