[英]How do I add a Google Font to a VueJS Component?
I've been trying for an hour to find a way to import a Google Font into a VueJS Component, but I cannot seem to find a solution, nothing worked yet, not even the stuff from previous StackOverflow questions.我已经尝试了一个小时来寻找一种将 Google 字体导入 VueJS 组件的方法,但我似乎找不到解决方案,还没有任何效果,甚至没有以前 StackOverflow 问题中的内容。 All the answers I've found are 1.5 to 2 years old now.
我找到的所有答案现在都是 1.5 到 2 岁。 I would appreciate it greatly if someone could suggest an up to date solution.
如果有人可以提出最新的解决方案,我将不胜感激。
I am using VueJS2 + Webpack + Vue-cli我正在使用 VueJS2 + Webpack + Vue-cli
The fastest way is to import the font in a CSS file, for example App.css
, if all components should have it:最快的方法是在 CSS 文件中导入字体,例如
App.css
,如果所有组件都应该有它:
@import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
html, body {
font-family: 'Roboto', sans-serif;
}
#app {
font-family: 'Roboto', sans-serif;
}
The import statement is also shown by Google Fonts.导入语句也由 Google Fonts 显示。
Select your fonts, click on Embed
and then @import
at the selection window:选择你的字体,点击
Embed
,然后在选择窗口中@import
:
Imo, if you're using VueJS, Google Fonts Webpack Plugin is the way. Imo,如果您使用的是 VueJS,那么 Google Fonts Webpack Plugin 就是最好的选择。
Here's the plugin , it's really easy to set it up and works like a charm.这是插件,设置起来真的很容易,而且效果很好。
npm i google-fonts-webpack-plugin -D
Go to your /webpack.config.js
/ webpack.base.config.js
and add the following lines:转到
/webpack.config.js
/ webpack.base.config.js
并添加以下行:
const GoogleFontsPlugin = require("google-fonts-webpack-plugin")
module.exports = {
"entry": "index.js",
/* ... */
plugins: [
new GoogleFontsPlugin({
fonts: [
{ family: "Source Sans Pro" },
{ family: "Roboto", variants: [ "400", "700italic" ] }
]
})
]
}
Now you can use Google Fonts anywhere inside your VueJS project :)现在您可以在 VueJS 项目中的任何地方使用 Google 字体 :)
I would like to add to the answer given by msqar.我想补充一下 msqar 给出的答案。 If you are going to use Google Fonts Webpack Plugin: ( https://www.npmjs.com/package/google-fonts-webpack-plugin ) and you are using the Vue CLI, you can add a vue.config.js file inside the root of your project.
如果您要使用 Google Fonts Webpack 插件:( https://www.npmjs.com/package/google-fonts-webpack-plugin )并且您正在使用 Vue CLI,您可以添加一个 vue.config.js 文件在项目的根目录中。 See Vue CLI docs: ( https://cli.vuejs.org/guide/webpack.html#simple-configuration )
请参阅 Vue CLI 文档:( https://cli.vuejs.org/guide/webpack.html#simple-configuration )
Then add the code to that file:然后将代码添加到该文件中:
const GoogleFontsPlugin = require("google-fonts-webpack-plugin");
module.exports = {
chainWebpack: config => {
plugins: [
new GoogleFontsPlugin({
fonts: [
{ family: "Source Sans Pro" }
]
})
]
}
}
I didn't see a good example of using web fonts locally in Vue.我没有看到在 Vue 本地使用网络字体的好例子。 So here is an example of what I used.
所以这是我使用的一个例子。 I have some SASS variables and web fonts defined in a CSS file that gets globally added to my app so I don't have to individually import each file into my components.
我在一个 CSS 文件中定义了一些 SASS 变量和 Web 字体,它们被全局添加到我的应用程序中,因此我不必单独将每个文件导入到我的组件中。 Note that the import for web fonts has a different syntax for the asset folder alias ~@/assets .
请注意,网络字体的导入对于资产文件夹别名~@/assets具有不同的语法。
vue.config.js vue.config.js
css: {
loaderOptions: {
sass: {
data: `
@import "@/assets/css/global.scss";
`
}
}
}
In my CSS file global.scss I have:在我的 CSS 文件 global.scss 中,我有:
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
font-display: swap;
src: local('OpenSans-Regular-400'), url(~@/assets/fonts/OpenSans-Regular-400.woff) format('woff');
}
/* color variables */
$corporate-blue: #005496;
With Vue3 and CLI使用 Vue3 和 CLI
For Vue 3 with CLI I am using the fork @beyonk/google-fonts-webpack-plugin to include the fonts locally.对于带有 CLI 的 Vue 3,我使用 fork @beyonk/google-fonts-webpack-plugin在本地包含字体。 This might also work with Vue2 and the latest CLI Version.
这也可能适用于 Vue2 和最新的 CLI 版本。 The original package does not work anymore.
原来的包已经不能用了。
npm i -D @beyonk/google-fonts-webpack-plugin
In your vue.config.js
add在你的
vue.config.js
添加
const GoogleFontsPlugin = require("@beyonk/google-fonts-webpack-plugin")
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
new GoogleFontsPlugin({
fonts: [
{ family: "Poppins", variants: [ "500", "700" ] }
]
})
]
}
}
Don't use the google-fonts-webpack-plugin
package nor the google-fonts-plugin
.不要使用
google-fonts-webpack-plugin
包或google-fonts-plugin
。 They don't work with Vue.它们不适用于 Vue。
Using @import
doesn't solve the problem neither, if you want to do a PWA and use the fonts offline.如果您想做 PWA 并离线使用字体,使用
@import
也不能解决问题。
The solution I used was to use fontsource , they have all Google Fonts.我使用的解决方案是使用fontsource ,他们有所有谷歌字体。 Just install the font you want with yarn and then import it in your SASS.
只需使用 yarn 安装您想要的字体,然后将其导入您的 SASS。
In Vue2, just @import the font in section inside your vue component在 Vue2 中,只需在 vue 组件内的部分中 @import 字体
<style>
@import url('https://fonts.googleapis.com/css?family=Proza+Libre');
h1 {
font-family: 'Proza Libre', sans-serif;
color: seagreen;
font-weight: 300;
}
</style>
I am currently doing it like the following:我目前正在这样做:
npm install --save typeface-source-sans-pro
)npm install --save typeface-source-sans-pro
)import 'typeface-titillium-web';
)import 'typeface-titillium-web';
)font-family: 'Titillium Web', sans-serif;
)font-family: 'Titillium Web', sans-serif;
) Keep in mind that with this the font gets self-hosted.请记住,这样字体就可以自托管了。 So you don't have the support of the cached fonts on a cdn any more.
因此,您不再支持 cdn 上的缓存字体。
I wrote in my css file我写在我的css文件中
@font-face {
font-family: "Assistant";
src: url("assistant/Assistant-VariableFont_wght.ttf") format("truetype");
}
I chose the format according to this: https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face我根据这个选择了格式: https ://developer.mozilla.org/en-US/docs/Web/CSS/@font-face
Then I added to the class of the app element:然后我添加到 app 元素的类中:
font-family: Assistant;
In the src part, you may use a link to the font if you don't have its files.在 src 部分,如果您没有字体文件,您可以使用字体链接。
As this seems to be something of a perennial question, another solution is to go to the base index.html, and paste in the refs that are also available from Google fonts. That also can include pre-loads which do speed up matters.由于这似乎是一个长期存在的问题,另一种解决方案是将 go 转换为基本索引 html,然后粘贴也可从 Google fonts 获得的参考文献。这也可以包括可以加快速度的预加载。 For example, on one site I have the following in the:
例如,在一个站点上,我有以下内容:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,400&family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,500&display=swap" rel="stylesheet">
(yeah, I know, Fira Sans, bite me: :-) ) (是的,我知道,Fira Sans,咬我::-))
That doesn't answer the OP's question exactly, as they asked about per-component addition of fonts, but this does supply a font ref that is site-wide.这并不能准确回答 OP 的问题,因为他们询问了 fonts 的每个组件添加,但这确实提供了站点范围的字体参考。 Anyway, it's another possibility.
不管怎样,这是另一种可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.