【发布时间】:2018-12-26 21:50:54
【问题描述】:
我想将本地字体导入我的 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'
【问题讨论】:
-
你有没有想过这个问题?我一直在寻找答案,但无法正常工作。对我来说,我在我的 url 中使用直接的 '/assets/typo/font.woff' 路径。它可以编译,但它没有显示在浏览器中
-
'/assets/typo/...'不起作用的原因是它找不到文件。这些文件需要与scss文件相关。 Webpack 将替换为正确的资产,具体取决于它是在 dev 中运行还是为构建而编译。