我想在我的vue-cli 3项目中导入本地字体。
.woff和.woff3文件位于src/assets/typo,并且我在src/scss/_typo.scss中包含它们:
我的 _typo.scss看起来像这样:
@font-face {
font-family: 'HKGrotesk';
src: url('@/assets/typo/HKGrotesk-Bold.woff2') format('woff2'),
url('@/assets/typo/HKGrotesk-Bold.woff') format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'HKGrotesk';
src: url('@/assets/typo/HKGrotesk-Medium.woff2') format('woff2'),
url('@/assets/typo/HKGrotesk-Medium.woff') format('woff');
font-weight: medium;
font-style: normal;
}
@font-face {
font-family: 'HKGrotesk';
src: url('@/assets/typo/HKGrotesk-Regular.woff2') format('woff2'),
url('@/assets/typo/HKGrotesk-Regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
这是我的vue.config文件,用于在全局范围内使用颜色和字体:
module.exports = {
css: {
loaderOptions: {
sass: {
data: `
@import "@/scss/_colors.scss";
@import "@/scss/_typo.scss";
`
}
}
}
};
当我运行我的项目时,我收到了以下错误信息:
Failed to compile.
./src/App.vue?vue&type=style&index=0&lang=scss& (./node_modules/css-loader??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/lib??ref--8-oneOf-1-2!./node_modules/sass-loader/lib/loader.js??ref--8-oneOf-1-3!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=style&index=0&lang=scss&)
Module not found: Error: Can't resolve './@/assets/typo/HKGrotesk-Bold.woff' in '/Users/robin/Documents/Code/2018/iamrobin-portfolio/src'
'/assets/typo/...'
无法正常工作的原因是它找不到文件。这些文件需要相对于scss
文件。Webpack 将根据其运行方式(开发或编译)替换为正确的资源文件。 - Daniel