使用webpack 2和vue-cli加载本地字体

9

我正在使用vue-cli webpack模板,并尝试在项目中加载本地字体。但是我无法正确获取字体路径。我的路径应该是什么样子的?

我找到了一些关于我可能做错的信息,但我还是弄不清楚:https://github.com/webpack-contrib/sass-loader#problems-with-url

文件结构:

enter image description here

在我的_fonts.scss中:

    // Webfont: LatoLatin-Regular
@font-face {
  font-family: 'LatoLatinWeb';
  src: url('../assets/fonts/LatoLatin-Regular.eot'); /* IE9 Compat Modes */
  src: url('../assets/fonts/LatoLatin-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
        url('../assets/fonts/LatoLatin-Regular.woff2') format('woff2'), /* Modern Browsers */
        url('../assets/fonts/LatoLatin-Regular.woff') format('woff'), /* Modern Browsers */
        url('../assets/fonts/LatoLatin-Regular.ttf') format('truetype');
  font-style: normal;
  font-weight: normal;
  text-rendering: optimizeLegibility;
}

Webpack.base.config.sj:

  {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
    }
  }

Utils.js:

  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass').concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/_variables.scss')
        }
      }
    ).concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/mixins/_mixins.scss')
        }
      }      
    ).concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/_fonts.scss')
        }
      }      
    ),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }

我该如何在vue-cli webpack模板中加载本地字体?
2个回答

13

我是这样导入字体的:

$font_path: '~@/assets/fonts/';

// *********** //
// SOURCE SANS //
// ITALIC
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Italic.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Italic.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Italic.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Italic.woff') format('woff'); /* Modern Browsers */
  font-style     : italic, oblique;
  font-weight    : 400;
  text-rendering : optimizeLegibility;
}
// REGULAR
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Regular.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Regular.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Regular.woff') format('woff'); /* Modern Browsers */
  font-style     : normal;
  font-weight    : 400;
  text-rendering : optimizeLegibility;
}
// SEMI BOLD
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Semibold.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Semibold.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Semibold.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Semibold.woff') format('woff'); /* Modern Browsers */
  font-style     : normal;
  font-weight    : 600;
  text-rendering : optimizeLegibility;
}
// Use "font-family: $source_sans_pro" in your code.
$source_sans_pro : 'Source Sans Pro', sans-serif;
// [END] SOURCE SANS //
// ***************** //

所有字体文件位于 src/assets/fonts/

我的webpack.base.conf文件包含以下加载器:

 {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
    }
  },

在我卡住并忘记相对路径不起作用后,这对我有用。谢谢! - Nick
太好了。不使用SVG字体将有助于包含SVG图像 :) - Joeri

1
在我正在开发的VueJS项目中,
这个没有起作用:
@font-face {
   font-family: 'name-of-choice';
   src: url("~@/assets/<location-to-your-font-file>/font-file-name.ttf") format('font-type');
}

This worked:

@font-face {
   font-family: 'name-of-choice';
   src: url(~@/assets/<location-to-your-font-file>/font-file-name.ttf) format('font-type');
}

我注意到,如果我们在一开始不使用双引号的解决方案,然后再添加双引号,它就能正常工作!如果有人知道这里发生了什么,请回答。

解决方案: 尝试不用引号包含URL字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接