Next.js 无法找到字体文件

huangapple go评论62阅读模式
英文:

Next.js unable to find font file

问题

我试图使用Next.js的本地字体功能,代码如下:

const Eina = localFont({
  src: "../public/fonts/eina01.ttf",
});

然而,无论我将eina文件或任何文件路径放在哪里,它都会出现以下错误:

Module not found: Can't resolve '@next/font/local/target.css?{"path":"pages/_app.js","import":"","arguments":[{"src":"../public/fonts/eina01.ttf"}],"variableName":"Eina"}'

它目前放在public/fonts目录中,但我尝试按照文档中的说明将它放在pages目录中也没有成功。我应该如何导入这个字体?

英文:

I'm trying to use next.js localFont, like so:

const Eina = localFont({
  src: "../public/fonts/eina01.ttf",
});

However, no matter where i place the eina file/whatever file path I try, it always gives me:

Module not found: Can't resolve '@next/font/local/target.css?{"path":"pages/_app.js","import":"","arguments":[{"src":"../public/fonts/eina01.ttf"}],"variableName":"Eina"}'

its currently placed in public/fonts, but I tried to place it in the pages like said in the docs, with no luck either. How am i meant to import the font?

答案1

得分: 1

I see that you have mentioned the path for font as public/font, but you have given the path in src as

src: "../public/fonts/eina01.ttf",

I think you just need to change it to

src: "../public/font/eina01.ttf",

Or, If it was just a typo in your question, please comment here.

英文:

I see that you have mentioned the path for font as public/font, but you have given the path in src as

src: "../public/fonts/eina01.ttf",

I think you just need to change it to

src: "../public/font/eina01.ttf",

Or, If it was just a typo in your question, please comment here.

答案2

得分: 0

以下是已翻译的内容:

一种选择是在 CSS 文件中导入字体文件,像这样:

@font-face {
    font-family: "Cutiful";
    font-style: normal;
    font-weight: 400;
    src: url("./cutiful.otf");
}

在此示例中,字体文件位于与 CSS 文件相同的目录中。
确保在 RootLayout 中导入 CSS 文件,使用 import "./globals.css" 或者您的 CSS 文件名称。
在 JSX 中,您可以像这样为标签添加类名:

<div className="myClass">some text in a div-tag</div>

然后,样式是在 CSS 文件中定义的,例如:

.myClass {
    font-family: "Cutiful", cursive;
    font-size: 3.4rem;
}

如果您坚持要使用 localFont,请查看这里:https://nextjs.org/docs/app/api-reference/components/font 或者这里:https://nextjs.org/docs/pages/api-reference/components/font

英文:

One option is to import the fontfile in a CSS-file like this:

@font-face {
    font-family: &quot;Cutiful&quot;;
    font-style: normal;
    font-weight: 400;
    src: url(&quot;./cutiful.otf&quot;);
}

In this example the fontfile is located in the same directory as the CSS-file.
Make sure to import the CSS-file in your RootLayout with import &quot;./globals.css&quot; or whatever the name of your CSS-file is. In your JSX you can then add classNames to tags like this:

&lt;div className=&quot;myClass&quot;&gt;some text in a div-tag&lt;/div&gt;

and the styling is done in the CSS-file again, for example:

.myClass {
    font-family: &quot;Cutiful&quot;, cursive;
    font-size: 3.4rem;
}

If you insist on using localFont, look here: https://nextjs.org/docs/app/api-reference/components/font or here: https://nextjs.org/docs/pages/api-reference/components/font

huangapple
  • 本文由 发表于 2023年6月29日 07:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定