英文:
Use @_components
问题
我有一个具有以下结构的 NextJS 13 应用程序:
app
_components
header.tsx
footer.tsx
login.tsx
(routes)
about
page.tsx
blog
page.tsx
contact
page.tsx
在我的 tsconfig.json 中,我有:
"paths": {
"@_*": ["../"]
}
想法是,例如在 contact page
中,我使用:
import Login from '@components/Login'
这不起作用。
同样在 _components
目录中,也不起作用。例如,如果我想在 "Header" 中导入 "Login",像这样:
import Login from '@components/Login'
我会得到模块找不到的消息。我明白在 _components
内部我在当前目录中,需要使用 ./
(../
用于父目录)。
挑战是如何正确编写 path
,以满足我的两种情况(_components 在当前目录和 _components 在父目录)?
英文:
I have a NextJS 13 app with this structure:
app
_components
header.tsx
footer.tsx
login.tsx
(routes)
about
page.tsx
blog
page.tsx
contact
page.tsx
In my tsconfig.json I have:
"paths": {
"@_*": ["../*"]
}
The idea is that for example in the contact page
I use:
import Login from '@components/Login'
This does not work.
Also within the _components
directory, it does not work. For example, if I want to import "Login" in "Header" like so:
import Login from '@components/Login'
I get the message that the module cannot be found. I understand that within _components
I am in the current dir and that I need to use ./
and ('../' is for the parent dir).
Challenge is how to write the path
correctly for both my scenarios (_components in current dir and _components in parent dir)?
答案1
得分: 0
假设您的 tsconfig.json
与 app
文件夹位于同一级目录。
尝试以下操作:
// tsconfig.json
"paths": {
"@/*": ["./app/*"]
}
然后像这样更新您的导入:
import Login from '@/_components/login'
英文:
Assuming your tsconfig.json
is on the same level as the app
folder.
Try the following:
// tsconfig.json
"paths": {
"@/*": ["./app/*"]
}
Then update your imports like so:
import Login from '@/_components/login'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论