如何使用nuxt js将字体嵌入到所有页面

问题描述 投票:0回答:6

我只是将谷歌字体嵌入到 nuxt.config.js 中的全局设置中

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

但是如何将这种字体应用到每个页面。

vue.js vuex nuxt.js
6个回答
33
投票

新答案

使用@nuxt/google-fonts包:https://nuxt.com/modules/google-fonts

使用起来很简单,并且支持很多选项,包括下载字体。


旧答案

如果您想在应用程序中包含该字体,以便您提供它

从以下位置下载字体:https://fonts.google.com/specimen/Roboto?selection.family=Roboto(打开抽屉并下载

.zip
文件)。

  1. 将.zip 的内容解压到
    ./assets/fonts/*
    (如果不存在则创建它)。
  2. 创建
    ./assets/fonts/roboto.css
    并将以下内容放入其中:
/* cyrillic-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: local('Roboto'), local('Roboto-Regular'), url(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(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(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(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(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(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(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;
}

(您可以通过访问 https://fonts.googleapis.com/css?family=Roboto 找到此 CSS)。

然后更改

nuxt.config.js
以将 CSS 文件包含在
css
属性中:

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

然后将

font-family: 'Roboto', sans-serif;
应用于文本元素。


在每个页面上使用该字体

创建一个

./assets/css/styles.css
文件,其中包含以下内容:

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

然后如上将CSS文件包含在

nuxt.config.js
中:

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

同样适用于任何资产,如其他字体、图标、框架 CSS 等。


24
投票

在你的 nuxt.config.js 中

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

并在您使用的 layouts/default.vue 或任何其他布局文件中添加样式标签

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

5
投票

就这么简单

  1. 转到 Google 字体并选择您的字体样式,例如 Montserrat

  2. 转到

    nuxt.config.js
    并添加您的字体网址,如下所示

export default {  
 head: {
        meta: [],
          link: [
       { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Montserrat&display=swap"'}
     ]
   }
  1. 转到你的 nuxt 项目 -> 布局目录 ->
    default.vue
    文件并在
    app
    标签中添加名为
    v-app
    (或任何你想要的名称)的类,如下所示
 <v-app dark class="app"> 

      --your app goes here -- 

  </v-app>
  1. 在相同的文件样式选项中,在上面定义的类主体中添加以下行

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

    确保 v-app 标签中定义了类,并使用

    <style>
    而不是
    <style scoped>
    来使整个项目生效


1
投票

使用以下规则更新您的 CSS:

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

如果您还没有设置 CSS 样式表,请在

assets
目录中创建一个名为
main.css
的新文件。将上面的代码放入其中并保存。然后编辑
nuxt.config.js
head
对象后面添加这段代码:

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

1
投票

我尝试了多种方法将自定义字体添加到 nuxt + vuetify 项目,但没有人能正确工作。

最后我在

global.scss
文件夹文件中创建了一个
assets
并在
nuxt.config.js
中解决它:

css: ['~assets/global.scss'],

并在

global.scss
文件中:

@font-face {
    font-family: 'my-custom-font';
    src: local('my-custom-font'), url('path/to/font') format('truetype');
}

.v-application {   
   font-family: 'my-custom-font' !important;
}

// or 

#app { 
    font-family: 'my-custom-font';
}

这工作正常。


0
投票

您可以执行以下步骤: 1__在assests目录中的单独文件中将字体定义为自定义变量,例如variables.scss,如下所示:

$body-font-family: Roboto;
@import '~vuetify/src/styles/styles.sass';

2__然后在 nuxt.config.js 中,你应该在头部添加一个样式表链接,如下所示:

head: {
  link: [
    { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Oswald|Libre+Baskerville&display=swap' }
  ]
},

3__那么需要在 vuetify 中启用 treeshaking 并设置自定义变量,如下所示:

vuetify: {
  customVariables: ['~/assets/variables.scss'],
  treeShake: true
},

© www.soinside.com 2019 - 2024. All rights reserved.