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

新手上路,请多包涵

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

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

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

原文由 Joey 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 781
1 个回答

新答案

使用@nuxt/google-fonts 包: https ://google-fonts.nuxtjs.org

它使用起来很简单,并且支持包括下载字体在内的一系列选项。


旧答案

如果你想在你的应用程序中包含字体以便你提供它

从以下网址下载字体: 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;
}

然后如上所述在 nuxt.config.js 中包含 CSS 文件:

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

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

原文由 Lawrence Cherone 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题