How to embed font to all page with nuxt js

时光毁灭记忆、已成空白 提交于 2020-05-08 11:18:28

问题


I just embed google font to the global setting in nuxt.config.js

 link: [
      { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Roboto' }
    ]

But how to apply this font to every page .


回答1:


If you want to include the font within your app so you're serving it

Download the font from: https://fonts.google.com/specimen/Roboto?selection.family=Roboto (Open the drawer and download the .zip file).

  1. Unpack the contents of the .zip into ./assets/fonts/* (create it if it does not exist).
  2. Create ./assets/fonts/roboto.css and place the following in it:
/* cyrillic-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu72xKOzY.woff2) format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu5mxKOzY.woff2) format('woff2');
  unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* greek-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu7mxKOzY.woff2) format('woff2');
  unicode-range: U+1F00-1FFF;
}
/* greek */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4WxKOzY.woff2) format('woff2');
  unicode-range: U+0370-03FF;
}
/* vietnamese */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu7WxKOzY.woff2) format('woff2');
  unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu7GxKOzY.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4mxK.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

(You can find this CSS by going to https://fonts.googleapis.com/css?family=Roboto).

Then change nuxt.config.js to include the CSS file in the css property:

module.exports = {
  /*
  ** Global CSS
  */
  css: [
    '~/assets/fonts/roboto.css'
  ]
}

Then you apply font-family: 'Roboto', sans-serif; to your text elements.


Using the font on every page

Create a ./assets/css/styles.css file with the following in it:

body
{
  font-family: 'Roboto', sans-serif;
}

Then as above include the CSS file in the nuxt.config.js:

module.exports = {
  css: [
    '~/assets/fonts/roboto.css',
    '~/assets/css/styles.css'
  ]
}

Same applies for any asset like other fonts, icons, framework css etc.




回答2:


QUICK

In your nuxt.config.js fi

head: {
  ...
  ...
  link: [
    {
      rel: 'icon',
      type: 'image/x-icon',
      href: '/favicon.ico'
    },
    {
      rel: 'stylesheet',
      href: 'https://fonts.googleapis.com/css?family=Lato:400,700'
    }
  ]
}

And in your layouts/default.vue or any other layout file that you are using add the style tag

<style scoped>
#app {
  font-family: 'Lato', sans-serif;
}
</style>



回答3:


It is simple like this

  1. Go to Google fonts and select your font style e.g Montserrat

  2. Got to nuxt.config.js and add your font url as shown here

export default {  
 head: {
        meta: [],
          link: [
       { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Montserrat&display=swap"'}
     ]
   }
  1. Go to your nuxt project -> layouts directory -> default.vue file and add class named app ( or whatever name you want) in v-app tag as shown here
 <v-app dark class="app"> 

      --your app goes here -- 

  </v-app>
  1. In the same file style option add the following lines in the class body you defined above

     <style>
     .app{
       font-family: 'Montserrat', sans-serif;
     }
     </style>
    

    Make sure class is defined in v-app tag and use <style> and not <style scoped> to take effect of whole project




回答4:


Update your CSS with the following rule:

body {
  font-family: 'Roboto', sans-serif;
}

If you don't have CSS stylesheet setup yet, create a new file in assets directory named i.e. main.css. Inside it put the code above and save it. Then edit nuxt.config.js adding this piece of code after head object:

css: [
  '@/assets/main.css'
],


来源:https://stackoverflow.com/questions/51436344/how-to-embed-font-to-all-page-with-nuxt-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!