简体   繁体   English

在 index.js 中重命名导出默认值

[英]rename export default in index.js

I have component called TModal that looks like this我有一个名为TModal的组件,看起来像这样

const TModal = () => {}

export default TModal

It lives inside src/components/Modal/Modal.js它位于src/components/Modal/Modal.js

next to it, there is an index.js在它旁边,有一个index.js

this is what index.js does:这就是 index.js 的作用:

export { TModal as Modal } from './Modal'

Then somewhere in my project, I want to import Modal like this然后在我项目的某个地方,我想像这样导入 Modal

import { Modal } from 'components/Modal'

but I keep getting但我不断得到

Attempted import error: 'Modal' is not exported from 'components/Modal'

What's the problem?有什么问题?


jsconfig.json jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "es6",
    "module": "commonjs",
    "jsx": "preserve",
    "paths": {
      "*": ["./*"]
    }
  },
  "include": ["src"]
}

strcuture结构

src
- components
-- Modal
--- index.js
--- Modal.js
- layouts
-- Auth
--- Auth.js

So as an example, I would want to use Modal inside Auth.js例如,我想在Auth.js中使用Modal

Since your Modal.js export default, your index.js should be written as below由于您的 Modal.js 导出默认值,因此您的index.js应编写如下

import Modal from './Modal';
export { Modal };

It looks like you are not selecting the file (Modal.js) you are just pointing at the Modal directory.看起来您没有选择您只是指向 Modal 目录的文件 (Modal.js)。 This should fix it if that is the problem.如果这是问题,这应该解决它。

import { Modal } from 'components/Modal/Modal'

The problem is with TModal .问题在于TModal When you export TModal from js file remove export keyword.当您从 js 文件中导出TModal时,请删除export关键字。 everything would be working fine.一切都会好起来的。 Help it helps.帮助它有帮助。

const TModal = () => {}
export TModal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 index.js 桶中导出默认模块 - How to export default modules in index.js barrels index.js' 不提供名为 'default' 的导出 - index.js' does not provide an export named 'default' 如何在 index.js 中重新导出多个命名导出和多个默认导出? - How to Re-Export Multiple Named Export and Multiple Default Export in index.js? ./src/index.js 尝试导入错误:“./App.js”不包含默认导出(导入为“App”) - ./src/index.js Attempted import error: './App.js' does not contain a default export (imported as 'App') 如何导出 index.js 中的所有组件? - How to export all components in index.js? index.js 尝试导入错误:“./App”不包含默认导出(导入为“App”) - index.js attempted import error: './App' does not contain a default export (imported as 'App') 无法导出 vue-router:警告 in./src/router/index.js “export 'default' (imported as 'VueRouter') is not found in 'vue-router' - Could not export vue-router: warning in ./src/router/index.js "export 'default' (imported as 'VueRouter') was not found in 'vue-router' React - 无法从 index.js 导出多个文件 - React - cannot export multiple files from index.js 如何在index.js中动态导出组件? - How can I dynamically export components in index.js? 将所有组件导出到 index.js 文件中是不好的做法吗? - Is it bad practice to export all components into an index.js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM