英文:
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: "Cutiful";
font-style: normal;
font-weight: 400;
src: url("./cutiful.otf");
}
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 "./globals.css"
or whatever the name of your CSS-file is. In your JSX you can then add classNames to tags like this:
<div className="myClass">some text in a div-tag</div>
and the styling is done in the CSS-file again, for example:
.myClass {
font-family: "Cutiful", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论