NextJS 在导入路径中的哪里定义波浪号“~”符号?

2024-01-13

我使用 NextJS 在应用程序中阅读代码。它导入组件,例如import Head from '~/components/layout/head'

项目结构:

-app
---components
---pages
---public

我想知道在哪里定义~作为 nextJS 中的根目录。

我在哪里可以找到这个的配置? 尝试在下一个包中找到 webpack 配置,但没有找到。


使用打字稿paths您可以指定模块映射功能。

// tsconfig.json

{
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
      "~/*": ["./src/*"]
    }
    ...
  }
}

这将允许您使用导入

import x from '~/components/layout/head';

将被映射到src/components/layout/head.

如果您使用的是 webpack,则需要使用tsconfig-paths-webpack-plugin https://github.com/dividab/tsconfig-paths-webpack-plugin.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NextJS 在导入路径中的哪里定义波浪号“~”符号? 的相关文章

随机推荐