59

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. All the answers I've found are 1.5 to 2 years old now. I would appreciate it greatly if someone could suggest an up to date solution.

I am using VueJS2 + Webpack + Vue-cli

4
  • You should say what you've tried so far Commented Jul 25, 2018 at 10:15
  • 3
    I cannot backtrace of what I've tried. I have tried solutions from every blog, stackoverflow, quora question, vuejs that had that topic, but all of them were for VueJS and not VueJS2. Cannot find anything on VueJS2.I'm sure any up to date solution would work Commented Jul 25, 2018 at 10:53
  • @André Turns out it was an impossible to see bug all the time that shouldn't be one. Two hours on a bug as usual. Thanks for taking your time to reply Andre. Commented Jul 25, 2018 at 10:56
  • 1
    The default @import way described in Google Fonts works fine (check App.vue): codesandbox.io/s/5yyp394zon | Also the <link> way (check index.html): codesandbox.io/s/40v3z6vy3x Commented Jul 25, 2018 at 11:30

10 Answers 10

106

The fastest way is to import the font in a CSS file, for example App.css, if all components should have it:

@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.

Select your fonts, click on Embed and then @import at the selection window:

Google Fonts

Sign up to request clarification or add additional context in comments.

1 Comment

If you're looking for a built-in solution for an offline PWA (rather than downloading the font every time), see Maurici Abad's solution below.
23

Imo, if you're using VueJS, Google Fonts Webpack Plugin is the way.

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:

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 :)

3 Comments

Does it makes "import" in the backgrounds (so, dynamic call to Google servers) or includes into the script bundle locally?
Yes it imports it. You can see that in the browser that it addes the fonts as a resource.
The original plugin does not work anymore. Also the syntax is outdated. See my answer here: stackoverflow.com/a/67272572/2440049
10

I would like to add to the answer given by 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. See Vue CLI docs: (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" }
                ]
            })
        ]
     }
 }

Comments

10

I didn't see a good example of using web fonts locally in 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. Note that the import for web fonts has a different syntax for the asset folder alias ~@/assets.

vue.config.js

css: {
    loaderOptions: {
      sass: {
        data: `
          @import "@/assets/css/global.scss";
        `
      }
    }
  }

In my CSS file global.scss I have:

@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;

2 Comments

Exactly what I needed ! Any idea how to add more than one font variant. like OpenSans-Regular-400, OpenSans-Bold-500 etc. ?
I'm trying your option because it's similar to mine.. it's working with the color, but with the font throw an error telling that this dependency was not found "* @/assets/fonts/Raleway_Regular.ttf in ./node_modules/css-loader/dist/cjs....." any idea?
7

Don't use the google-fonts-webpack-plugin package nor the google-fonts-plugin. They don't work with Vue.

Using @import doesn't solve the problem neither, if you want to do a PWA and use the fonts offline.

The solution I used was to use fontsource, they have all Google Fonts. Just install the font you want with yarn and then import it in your SASS.

3 Comments

Best solution imo. Everything should be bundled inside for production.
Usage example. CLI: yarn add @mdi/[email protected] yarn add @fontsource/roboto import at main.js: import '@mdi/font/css/materialdesignicons.css' import '@fontsource/roboto/100.css' import '@fontsource/roboto/300.css' import '@fontsource/roboto/400.css' import '@fontsource/roboto/500.css' import '@fontsource/roboto/700.css' import '@fontsource/roboto/900.css'
Documentation is great: fontsource.org
6

With Vue3 and CLI

For Vue 3 with CLI I am using the fork @beyonk/google-fonts-webpack-plugin to include the fonts locally. This might also work with Vue2 and the latest CLI Version. The original package does not work anymore.

npm i -D @beyonk/google-fonts-webpack-plugin

In your vue.config.js add

const GoogleFontsPlugin = require("@beyonk/google-fonts-webpack-plugin")

// vue.config.js
module.exports = {
    configureWebpack: {
        plugins: [
            new GoogleFontsPlugin({
                fonts: [
                    { family: "Poppins", variants: [ "500", "700" ] }
                ]
            })
        ]
    }
}

2 Comments

can please someone confirm if it also works with Vue2?
If I want google sans in all the 300-800 weights what is the line for family? This doesn't work: { family: "Open Sans", variants: [ "300", "400", "500", "600", "700", "800" ] }
4

In Vue2, just @import the font in section inside your vue component

<style>
@import url('https://fonts.googleapis.com/css?family=Proza+Libre');
h1 {
    font-family: 'Proza Libre', sans-serif;
    color: seagreen;
    font-weight: 300;
}
</style>

Comments

2

I am currently doing it like the following:

  • Install the typeface of the font via npm (eg: npm install --save typeface-source-sans-pro)
  • Import the typeface in the component (import 'typeface-titillium-web';)
  • Use the font-face in the component (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.

Comments

1

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. 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! :-) )

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. Anyway, it's another possibility.

Comments

0

I wrote in my css file

@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

Then I added to the class of the app element:

font-family: Assistant;

In the src part, you may use a link to the font if you don't have its files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.