首页 > 解决方案 > 从“”导入 {sub} VS 从“”导入 M

问题描述

export default { HolyClickoutside, ... } // "holy-components"

当我从“神圣组件”中导入我需要的东西时

import HolyComponents from "holy-components"
const { HolyClickoutside } = HolyComponents

console.log(HolyClickoutside)    // {...}

没问题

import { HolyClickoutside } from "holy-components" 

console.log(HolyClickoutside)    // undefined

导出解构对象不起作用,为什么?

标签: javascriptimport

解决方案


以下行仅获取默认情况下在“holy-components”中导出的变量

import HolyComponents from "holy-components"; // get whatever is exported by default in HolyComponents.

下面的行只查找名为 HolyClickoutside 的变量,它可以是“holy-components”中的许多命名导出之一。由于不存在具有特定名称的此类变量,因此您将得到未定义。

import { HolyClickoutside } from "holy-components" // get the variable exported from "holy-components" which has the exact name as HolyClickOutside.

下面的行在名为 HolyComponents 的对象中查找名为 HolyClickOutside 的键。如果默认情况下从“holy-components”导出的变量具有该键,您将获得对该键的引用。

const { HolyClickoutside } = HolyComponents

推荐阅读