【问题标题】:Custom Bootstrap 4 breakpoint not seen in component-level styles in Angular CLI app在 Angular CLI 应用程序的组件级样式中看不到自定义 Bootstrap 4 断点
【发布时间】:2019-03-24 08:13:09
【问题描述】:

在向 $grid-breakpoints 添加新断点后,我在使用 Bootstrap 的 media-breakpoint-up mixin 声明 scss 样式时遇到问题。

虽然 d-{infix}-{display} 类正确出现并且包含它们的部分运行良好,但组件级 scss 样式没有看到新断点,尽管导入。

我的问题是 - 我在这里做错了吗?比如导入顺序?

具有这两个依赖项的 Angular CLI 应用程序:

"bootstrap": "^4.1.3",
"ngx-bootstrap": "^3.0.1",

styles.scss

@import 'scss/my.variables';
@import 'scss/my.theme';
@import 'scss/bootstrap.partials';

my.variables.scss(无关紧要)

$brand-primary:#00a651
$fixed-header-min-height: 70px;    
$box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
$box-shadow-hover: 0 4px 12px 0 rgba(0, 0, 0, 0.3);

my.theme.scss

$font-family-sans-serif:  proxima-nova, Helvetica, Arial, sans-serif;
$font-family-monospace:   Menlo, Monaco, Consolas, "Courier New", monospace;
$font-family-base:        $font-family-sans-serif;
$font-weight-normal:      200;
$font-weight-base:        $font-weight-normal;
$body-bg:                 #fff;

bootstrap.partials.scss

$grid-breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px,
  xxl: 1600px
);

$container-max-widths: (
  sm: 540px,
  md: 720px,
  lg: 960px,
  xl: 1140px,
  xxl: 1540px
);

// Bootstrap partial imports to keep the bloat away
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~bootstrap/scss/reboot';
@import '~bootstrap/scss/type';
@import '~bootstrap/scss/images';
@import '~bootstrap/scss/grid';
@import '~bootstrap/scss/utilities';
@import '~bootstrap/scss/buttons';
@import '~bootstrap/scss/transitions';
@import '~bootstrap/scss/modal';
@import '~bootstrap/scss/close';
@import '~bootstrap/scss/nav';
@import '~bootstrap/scss/navbar';

app.components.html

<h1>Custom breakpoint - xxl introduced (1600px)</h1>

<h2>media queries (media-breakpoint-up et al.)</h2>
<div class="main">
  <p>
    this text should be blue, but red above xxl
  </p>

  <small>
    This Text Should All Be Lowercase, but Uppercase above xxl
  </small>
</div>

<h2>d-* classes</h2>

<p class="d-block">Visible always</p>
<p class="d-none d-lg-block">Visible above lg</p>
<p class="d-none d-xxl-block">Visible above xxl</p>

app.component.scss

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import '~bootstrap/scss/mixins/_breakpoints';

.main {
  color: blue;
  @debug $grid-breakpoints;

  @include media-breakpoint-up(xxl) {
    /* I have also tried without repeating the selector */
    .main {
      color: red;
    }
  }

  small {
    text-transform: uppercase;
    @include media-breakpoint-up(xxl) {
      small {
        text-transform: lowercase;
      }
    }
  }
}

输出

DEBUG: (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)        

没有 xxl。

编辑 1:

我创建了一个自定义包装器 mixin,它确认没有找到断点。

@mixin respond-above($breakpoint) {
  @if map-has-key($breakpoints, $breakpoint) {
    @include media-breakpoint-up($breakpoint){
      @content;
    }
  } @else {
    @warn 'Missing breakpoint: #{$breakpoint}.';
  }
}

【问题讨论】:

    标签: angular twitter-bootstrap sass bootstrap-4 scss-mixins


    【解决方案1】:

    我可以想到两件可能的事情。

    1) 您在全局 styles.scss 中包含 bootstrap.partials';,但是如果在其中定义了任何变量或 mixin,它们将被“编译”出最终的全局 css,并且在组件中不可用部分。因此,您可能必须将其包含在您的 app.component.scss

    2) 你的angular.json 在根目录中是什么样子的?尝试将node_modules引导目录的路径添加到angular.json中的stylePreprocessorOptions.includePaths[]

    {
        "projects": {
            "myProject": {
                "architect": {
                    "build": {
                        "options": {
                            "stylePreprocessorOptions": {
                                "includePaths": [
                                    "node_modules/bootstrap"
                                ]
                            },
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 非常感谢,miir!它有帮助。虽然(2)没有改变任何东西,但(1)做了很多!我将尝试重新组织文件,因为将整个引导存储桶导入每个文件感觉很糟糕。顺便说一句,stylePreprocessorOptions 现在位于architect/build/options 内。接受你的回答。
    • 没问题,很高兴有帮助!是的,我同意,在任何地方都包含它可能存在一些问题,特别是如果有 css 规则,因为这些规则会被重复,所以如果可以隔离 mixins/variables 并只导入它们,那将是最好的。这是我自己一直在考虑的事情,我不确定最好的解决方案是什么。我想知道将它放入顶级根组件 scss 是否足够?也许不是,因为组件样式应该与其他组件隔离,而 scss 仍然会编译并丢失 var 和 mixins。
    • 可悲的是,当我导入部分存储桶时,(!) main.js 包 (js!!!) 的包大小增加了约 300kb。当我查看 js 代码时,有一些 jQuery('on' listeners)和缓动函数的痕迹。我很困惑。目前这不是一个解决方案:(
    • 哦,不……那很不幸! :( 我认为另一种选择是绕过 CLI 的延迟加载,并使用更传统的 sass-build 设置和 gulp 或 grunt 任务......或者将引导程序部分隔离到只有 mixins/variables,并且只包含那些?
    猜你喜欢
    • 2018-03-20
    • 2019-09-19
    • 1970-01-01
    • 2020-05-01
    • 2018-01-14
    • 2021-03-14
    • 1970-01-01
    • 2023-04-04
    • 2017-09-09
    相关资源
    最近更新 更多