How do I add a Google Font to a VueJS Component?

前端 未结 7 1916
离开以前
离开以前 2021-02-03 20:42

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 pre

7条回答
  •  盖世英雄少女心
    2021-02-03 21:32

    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;
    

提交回复
热议问题