使用 @_components

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

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.jsonapp 文件夹位于同一级目录。

尝试以下操作:

// 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'

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

发表评论

匿名网友

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

确定