将模块导入 React 项目时 ./ 和 $ 的区别

Diference between ./ and $ when importing modules into React project

this project 的作者使用以下路径导入了一个组件:

import { client } from '$lib/graphql-client'

我的问题是: $符号有什么作用?它和类似的东西有什么区别:import {client} from './lib/graphql-client' ?

这只是一个别名。在此特定示例中,它解析为 ./src/lib.

https://github.com/GraphCMS/graphcms-sveltekit-portfolio-and-blog-starter/blob/ca2b4175c312def64fb7965db860acd21f2ec707/svelte.config.js#L13

这是路径别名。

路径别名: 使用路径别名,您可以为某些路径声明别名。

如您在 jsconfig 中所见,在 compilerOptions 中,您有:

1- baseUrl:告诉编译器从哪里开始解析相对导入。

2- paths: 定义特定路径的别名。

这对于写长而重复的路径很有用。