如何在Angular 4项目中导入外部字体?

时间:2017-10-13 09:26:43

标签: angular less

我的ng项目的根目录中有styles.less我要导入谷歌字体:

@import url('https://fonts.googleapis.com/css?family=PT+Sans:400,700&subset=cyrillic');

但是错误发生了:

Failed to compile.

./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader?{"ident":"postcss"}!./node_modules/less-loader/dist/cjs.js?{"sourceMap":false}!./src/styles.less
Module build failed: 

@import url('https://fonts.googleapis.com/css?family=PT+Sans:400,700&subset=cyrillic');

^
Can't resolve './https://fonts.googleapis.com/cssfamily=PT+Sans:400,700&subset=cyrillic' in 'C:\apps-dev\ldrm\src' in C:\apps-dev\ldrm\src\styles.less (line 1, column 0)

@./src/styles.less 4:14-187

@multi ./src/styles.less

2 个答案:

答案 0 :(得分:2)

Angular正在此次调用中添加“./”。请参阅here原因。

您有两个选择:

  1. 在index.html中包含该字体
  2. 下载字体,将其放在assets文件夹中并按如下方式调用:

    @font-face {
        font-family: 'your-font-family';
        src: url('/assets/fonts/your-donwloaded-font-1'), 
             url('/assets/fonts/your-donwloaded-font-2');
    }
    

答案 1 :(得分:0)

您可以尝试这种方式:

@chosen-font: 'PT Sans';
@chose-font-escaped: escape(@chosen-font);
@import url('https://fonts.googleapis.com/css?family=@{chose-font-escaped}:400,700&subset=cyrillic');