为什么import store from ‘./store‘和 ‘./store/index‘一样

import store from './store'

这种方式假定在当前文件所在目录下有一个名为store的文件(可以是JavaScript或TypeScript文件,取决于你的项目配置)。当你只导入目录路径时,默认会查找该目录下的index.jsindex.ts文件作为入口

比如这样的结构

- src
  - store
    - index.js
    - ...
- ...

那么import store from './store'将会导入store目录下的index.js文件导出的内容。

import store from './store/index'

这种方式直接指定了要导入的文件路径为./store/index,会导入store目录下的index.js文件的内容。

这两种方式会导入相同的内容,因为在一个目录中,通常只会有一个index.js文件作为入口,所以即使你不写index它也会默认去找到index入口文件

猜你喜欢

转载自blog.csdn.net/m0_64880608/article/details/133190153