【问题标题】:How to get hue/lighter color from Primary Palette如何从 Primary Palette 获得色调/较浅的颜色
【发布时间】:2017-07-12 19:07:51
【问题描述】:

所以在 Angular Material 2 中,我设置了我的主题

$store-primary: mat-palette($mat-storecast);
$store-accent:  mat-palette($mat-pink, A200, A100, A400);

// The warn palette is optional (defaults to red).
$store-warn:    mat-palette($mat-red);

// Create the theme object (a Sass map containing all of the palettes).
$store-theme: mat-light-theme($store-primary, $store-accent, $store-warn);

// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($store-theme);

我无法从他们的文档中弄清楚如何从主调色板(即按钮上的工具栏)获取不同的色调。

<button md-mini-fab (click)="zoomIn()" color="primary">
     <md-icon>add</md-icon>
</button>

它似乎只理解 color=primary 或 color=accent 等。你如何指定我想使用 primary-100 或 primary-500 等?

【问题讨论】:

    标签: angular angular-material angular-material2


    【解决方案1】:

    对我来说,我通过在组件 scss 中创建自定义 mixin 来解决此问题。在这个 mixin 中,您可以通过调用 map-get 方法来使用原色。您需要在您的styles.scss 中包含mixin(您包含angular-material-theme)才能完成这项工作。请参见下面的示例。

    从你的组件 scss 创建一个自定义 mixin:

    @import '~@angular/material/theming';
    
    @mixin custom-component($theme) {
      $primary: map-get($theme, primary);
    
      mat-toolbar{
        &.p100{
          background-color: mat-color($primary, 100);
        }
      }
    }
    

    将模板中的工具栏指定为“p100”类:

    <mat-toolbar class="p100"></mat-toolbar>
    

    最后将 mixin 包含在您的 styles.scss 中,以便将 mixin 包含在您的主题中:

    @import 'PATH_TO_YOUR_COMPONENT/custom-component.scss'; <--------- import the custom mixin
    
    $store-primary: mat-palette($mat-storecast);
    $store-accent:  mat-palette($mat-pink, A200, A100, A400);
    
    // The warn palette is optional (defaults to red).
    $store-warn:    mat-palette($mat-red);
    
    // Create the theme object (a Sass map containing all of the palettes).
    $store-theme: mat-light-theme($store-primary, $store-accent, $store-warn);
    
    // Include theme styles for core and each component used in your app.
    // Alternatively, you can import and @include the theme mixins for each component
    // that you are using.
    @include custom-component($store-theme); <--------- Include it before you include angular-material-theme
    @include angular-material-theme($store-theme);
    

    【讨论】:

      【解决方案2】:

      官方的回答是,目前这是不可能的。大多数组件上可用的 color 属性仅接受调色板类型 - 即主要、重音或警告。

      如果您真的想这样做,非官方的回答是,如果您不介意滥用内部 API,则可以(逐个组件地)。例如,要在按钮上获得 A100 颜色,您可以将自定义 mixin 添加到您的主题中。

      // custom-button-theme.scss
      @import '~@angular/material/core/theming/all-theme';
      @import '~@angular/material/button/_button-theme';
      
      @mixin custom-button-theme($theme) {
      
        .mat-raised-button.a100, .mat-fab.a100, .mat-mini-fab.a100 {
          // _mat-button-theme-color is a mixin defined in _button-theme.scss
          // which applies the given property, in this case background-color
          // for each of the palettes - i.e. primary, accent and warn.
          // The last parameter is the hue colour to apply.
          @include _mat-button-theme-color($theme, 'background-color', 'A100');
        }
      
      }
      

      然后在主主题文件中导入自定义 mixin。

      @import 'custom-button-theme.scss';
      @include custom-button-theme($store-theme);
      

      然后您的按钮可以通过添加 a100 类来使用颜色。

      <button md-mini-fab (click)="zoomIn()" color="primary" class="a100">
        <md-icon>add</md-icon>
      </button>
      

      需要注意的是,内部 API 可以随时更改。此代码已使用 2.0.0-beta.2 版本进行了测试 - 无法保证它在测试版 3 发布后也可以使用。

      【讨论】:

      • 好的,谢谢伊恩,我想这将在不久的将来添加。
      【解决方案3】:

      我使用 angular-material 1,我不确定是否是相同的方式,但我所做的是使用指令:md-colors="::{background: 'themename-primary-100'}" 当然在themename 中输入主题名称:P

      【讨论】:

      • 我试过喜欢 color="primary-100" 但由于某种原因它变成了重音主题。很奇怪
      • 你需要定义主题,在 Angular 1 中是这样完成的:$mdThemingProvider.theme('themename') .primaryPalette('blue-grey') .accentPalette('lime') .warnPalette('red'); $mdThemingProvider.setDefaultTheme('themename'); 然后你使用md-colors="::{background: 'themename-primary-100'}"
      • 请,如果正确,请标记答案,我在答案字段中将其全部编辑,以便更具可读性。
      • @LuizRossi 这不正确。他专门询问了angular2和material2。
      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 2010-09-10
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      相关资源
      最近更新 更多