Raj*_*Raj 7 javascript less webpack vue.js vuejs2
我做了很多阅读,但没有一个链接告诉我如何在 vuejs 中添加字体。这就是我在我的 less 文件中导入字体的方式。
@font-face {
font-family: "Questrial";
src: url("../../fonts/Questrial-Regular.ttf");
}
@font-face {
font-family: "Galano_Grotesque_extra_Bold";
src: url("../../fonts/Galano_Grotesque_Bold.otf");
}
@font-face {
font-family: "Galano_Grotesque_Bold";
src: url("../../fonts/Galano_Grotesque_DEMO_Bold.otf");
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
./src/themes/jatango-theme/components/fonts/Galano_Grotesque_Bold.otf 中的错误 1:4 模块解析失败:意外字符 '' (1:4) 您可能需要适当的加载程序来处理此文件类型。(此二进制文件省略了源代码)
我是 VUEJS 的新手,之前对 react 或 angular 一无所知。我只知道 jquery 和 javascript。所以请有人给我一个包含字体的分步指南。提前致谢 :)


将您的字体放在public/fonts/文件夹中。在 css 或 scss 中,指定以/fonts/.
示例scss:
$font-dir: "/fonts/";
@font-face {
font-family: "NameFont";
src: url("#{$font-dir}NameFontRegular/NameFontRegular.eot");
src: url("#{$font-dir}NameFontRegular/NameFontRegular.eot?#iefix")format("embedded-opentype"),
url("#{$font-dir}NameFontRegular/NameFontRegular.woff") format("woff"),
url("#{$font-dir}NameFontRegular/NameFontRegular.ttf") format("truetype");
font-style: normal;
font-weight: normal;
}
Run Code Online (Sandbox Code Playgroud)
接下来,将您的 scss 导入 main.js
例子:
// eslint-disable-next-line
import styles from './assets/scss/main.scss';
Run Code Online (Sandbox Code Playgroud)
或在 vue.config.js
例子:
module.exports = {
...
css: {
modules: true,
loaderOptions: {
css: {
localIdentName: '[name]-[hash]',
camelCase: 'only'
},
sass: {
data: '@import "~@/assets/scss/import/_variables.scss"; @import "~@/assets/scss/import/_mixins.scss";'
},
},
},
...
}
Run Code Online (Sandbox Code Playgroud)
希望这能帮助其他面临同样问题的人。由于某种原因,Vue.js 在使用文件时出现错误.otf。我使用过.woff,现在一切正常。在您的文件中使用以下代码webpack.config.js:
module.exports = function (config, { isClient, isDev }) {
module: { rules: [ { test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
loader: 'file-loader' } ] }
return config }
Run Code Online (Sandbox Code Playgroud)
并使用类似这样的方法导入 css 文件中的文件
@font-face {
font-family: "Questrial";
src: url("../../fonts/Questrial-Regular.ttf");
}
Run Code Online (Sandbox Code Playgroud)
这是一个 webpack 错误。您缺少一个webpack 加载器来管理字体文件。通常我使用文件加载器来加载字体:
{
test: /\.(ttf|otf|eot|woff|woff2)$/,
use: {
loader: "file-loader",
options: {
name: "fonts/[name].[ext]",
},
},
}
Run Code Online (Sandbox Code Playgroud)
将此代码添加到您的webpack 配置文件(模块 > 规则部分)中,或者如果您使用的是 vue-cli 3,则添加到vue.config.js文件中(configurewebpack 部分)。
| 归档时间: |
|
| 查看次数: |
15382 次 |
| 最近记录: |