如何导入scss变量取决于包含的mixin

时间:2019-08-07 17:02:36

标签: angular sass scss-mixins

我的棱角项目中具有以下结构:

- modules
 -- foo-module
   -- foo-comp
- assets
 -- fonts
   -- Open_Sans
      ....
- scss
 -- partials
   -- _fontface.scss
   -- _variables.scss
 styles.scss

styles.scss:

@import "./partials/variables";

* {
  margin:  0;
  padding: 0;
}

body{
    background-color: $color;
    font-family: $font-face;
}

_variables.scss:

@import "./fontface";

@include font-face(OpenSans, "../fonts/Open_Sans/OpenSans-Regular", 500, normal, ttf);

$font-face: OpenSans, sans-serif;
$color: yellow;

_fontface.scss:

    // =============================================================================
// String Replace
// =============================================================================

@function str-replace($string, $search, $replace: "") {
    $index: str-index($string, $search);

    @if $index {
        @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
    }

    @return $string;
}

// =============================================================================
// Font Face
// =============================================================================

@mixin font-face($name, $path, $weight: null, $style: null, $exts: eot woff2 woff ttf svg) {
    $src: null;

    $extmods: (
        eot: "?",
        svg: "#" + str-replace($name, " ", "_")
    );

    $formats: (
        otf: "opentype",
        ttf: "truetype"
    );

    @each $ext in $exts {
        $extmod: if(map-has-key($extmods, $ext), $ext + map-get($extmods, $ext), $ext);
        $format: if(map-has-key($formats, $ext), map-get($formats, $ext), $ext);
        $src: append($src, url(quote($path + "." + $extmod)) format(quote($format)), comma);
    }

    @font-face {
        font-family: quote($name);
        font-style: $style;
        font-weight: $weight;
        src: $src;
    }
}

comp.scss:

@import'..... / variables';

现在我想在我的 comp.scss 中使用 _variables.scss 作为导入。所以它看起来不太可能,因为@include font-face(OpenSans, "../fonts/Open_Sans/OpenSans-Regular", 500, normal, ttf);这个mixin的URL不依赖 comp.scss URL,所以我有一个对应的错误。而且看起来在style.scss(../网址的原因)中很好

我需要此全局变量 $ font-face 以便将来使用多种字体,并且我想将它们存储在一个位置。

1 个答案:

答案 0 :(得分:0)

自Angular 6起,您似乎可以对~使用绝对导入

@include font-face(OpenSans, "~src/assets/fonts/Open_Sans/OpenSans-Regular", 500, normal, ttf);