角度材料排版设置不正确

L1g*_*ira 8 typography sass angular-material angular angular-material-theming

我正在尝试实施 Angular Material Typography,但是我遇到了一些困难,我不确定可能是什么问题。

我按照 Angular在此处提供的说明进行操作,但是遇到了问题。问题是看起来应用程序中的所有内容都受到了影响,但是有些内容被看起来像默认排版的东西覆盖了,我不确定我做错了什么。

创建和使用的排版

$typography-configuration: mat-typography-config(
  $font-family: 'Roboto, "Helvetica Neue", sans-serif',
  $display-4: mat-typography-level(112px, 112px, 300),
  $display-3: mat-typography-level(100px, 56px, 400),
  $display-2: mat-typography-level(100px, 48px, 400),
  $display-1: mat-typography-level(100px, 34px, 400),
  $headline: mat-typography-level(100px, 32px, 400),
  $title: mat-typography-level(100px, 32px, 500),
  $subheading-2: mat-typography-level(100px, 28px, 400),
  $subheading-1: mat-typography-level(100px, 24px, 400),
  $body-2: mat-typography-level(100px, 24px, 500),
  $body-1: mat-typography-level(100px, 22px, 400),
  $caption: mat-typography-level(100px, 22px, 400),
  $button: mat-typography-level(100px, 14px, 500),
  $input: mat-typography-level(100px, 1.125, 400)
);

@include angular-material-typography($typography-configuration);
Run Code Online (Sandbox Code Playgroud)

正如您在排版中看到的100px那样,我故意设置了第一个参数(字体大小),以便我可以查看应用程序上的所有内容是否都受到影响。

示例 #1:材质按钮

对于 Angular Material 按钮(在 html 元素“button”上使用“mat-button”),我看到了我所期望的,如下所示。文本被设置为 100px。 在此处输入图片说明

示例 2:工具栏

但是对于垫子工具栏,我没有看到字体大小设置为 100px。在我看来,如下所示的默认字体大小覆盖了 100px 大小:

在此处输入图片说明

示例 #3:具有多重覆盖的不同工具栏

对于另一个工具栏上的最后一个示例,我们可以看到 100px 被添加了几次,每次都相互覆盖,直到最后一个覆盖的也是 20px,因此字体大小不是 100px。

在此处输入图片说明

细节:

此处的 Angular Material 指南显示我们有 3 种方法可以在应用程序中使用排版:

// Override typography CSS classes (e.g., mat-h1, mat-display-1, mat-typography, etc.).
@include mat-base-typography($custom-typography);

// Override typography for a specific Angular Material components.
@include mat-checkbox-typography($custom-typography);

// Override typography for all Angular Material, including mat-base-typography and all components.
@include angular-material-typography($custom-typography);
Run Code Online (Sandbox Code Playgroud)

据我了解,我使用的第三个选项应该适用于您使用class="mat-typography". 所以我将此添加到body应用程序的app-root. 但是,正如我们在上面看到的,只有部分应用程序受到正确影响。

我尝试将它添加到组件中应用程序中较小的部分,但结果是一样的。

我不确定我的理解是否错误并且我没有正确使用它或者其他地方是否存在问题,我假设我使用它错误但是我找不到任何其他人创建的教程和示例。

我尝试将自己的类附加到诸如class="mat-display-1"似乎工作正常的项目,但是如果我必须在整个应用程序的任何地方应用它,这似乎不是一个好的解决方案。

如果可能的话,我希望它可以在整个应用程序中应用,而不必单独为排版设置 100 个类。

我知道我可以按照这里的建议应用我自己的 sass 元素,但是我认为他们自己做。

L1g*_*ira 15

这可能是由于与 sass 文件的顺序及其调用的函数有关的一些原因造成的:

原因:

发生这种情况的原因是由于 Angular Material_theming.scss文件调用 sass mixins 的顺序,该文件可以在node_modules\@angular\material\_theming.scss.

麻烦的 mixinmat-coreangular-material-typography.

使用主题:

当您创建一个主题并调用 Angular Material 的主题mat-core混入时,它包含许多混入。

@mixin mat-core($typography-config: null) {
  @include angular-material-typography($typography-config);
  @include mat-ripple();
  @include cdk-a11y();
  @include cdk-overlay();
  @include cdk-text-field();
}
Run Code Online (Sandbox Code Playgroud)

正如您在上面的代码片段中看到的那样,angular-material-typography确实调用了 mixin。当mat-core被调用时正在寻找排版配置,如果没有提供,它将使用默认配置。

使用排版:

当您仅使用排版文件时,通常会看到如下所示的内容:

@import '~@angular/material/theming';

$typography-configuration: mat-typography-config(
  $font-family: 'Roboto, "Helvetica Neue", sans-serif',
  $display-4: mat-typography-level(112px, 112px, 300),
  $display-3: mat-typography-level(100px, 56px, 400),
  $display-2: mat-typography-level(100px, 48px, 400),
  $display-1: mat-typography-level(100px, 34px, 400),
  $headline: mat-typography-level(100px, 32px, 400),
  $title: mat-typography-level(100px, 32px, 500),
  $subheading-2: mat-typography-level(100px, 28px, 400),
  $subheading-1: mat-typography-level(100px, 24px, 400),
  $body-2: mat-typography-level(100px, 24px, 500),
  $body-1: mat-typography-level(100px, 22px, 400),
  $caption: mat-typography-level(100px, 22px, 400),
  $button: mat-typography-level(100px, 14px, 500),
  $input: mat-typography-level(100px, 1.125, 400)
);

@include angular-material-typography($typography-configuration);
Run Code Online (Sandbox Code Playgroud)

从上面的代码片段可以看出,我们创建了一个排版配置。以及我们称之为 Angular Material 的主题 mixin typography-configuration。这将为您设置排版,但是如果您同时使用 Angular Material Theme 和 Angular Material Typography,您可能会遇到排版被覆盖的情况。这就是订单的问题所在。

不正确的实施导致您的问题:

@import './_theme-master.scss';
@import './theme-typography';
@include mat-core();
Run Code Online (Sandbox Code Playgroud)

例如上面的例子:假设在你的styles.scss文件中你导入了一个主题文件,其中包含设置 aa 主题调色板并调用@include angular-material-theme($theme);. 在此之后,您决定导入您的排版文件。您的排版文件具有排版配置以及您调用@include angular-material-typography($typography-configuration);传入配置的方法。

现在完成这些之后,您决定调用您的mat-core. 此时,您的样式已为您的排版创建,您很高兴,但是,当调用mat-coreAngular 时,会使用默认的排版配置创建另一组样式。现在这些是最新的样式,因此您的样式会被创建的最新样式覆盖。

本质上,您的调用 mixin 如下所示:

@include angular-material-typography($typography-configuration);
@include mat-core();
Run Code Online (Sandbox Code Playgroud)

要解决您的问题:

而是像这样加载您的排版配置:

@include mat-core($typography-configuration);
Run Code Online (Sandbox Code Playgroud)

现在是创建许多排版样式并相互覆盖的问题。这很可能是由于一个文件(可能是包含您的排版的文件)被多次导入导致 mixin 调用了几次。尝试将您的排版文件放在 1 个中央空间中,就像调用一次文件一样styles.scss

  • 一直有效到角度 14/材质 14。在版本 15 中,调用 core() 时无法再传递版式参数。我将其替换为他们在文档中所做的操作,但它不起作用,而且我的应用程序已损坏...... (16认同)