不知道為什么這不起作用。我曾經有
// file 1
import Box from '@/components/Box'
import Popup from '@/components/Popup'
const MDXComponents = {
Box,
Popup
}
// using MDXComponents somewhere in file 1
現在我想外包 MDXComponents 物件,因為它變得太大了。所以我創建了一個新檔案:
// file 2
import Box from '@/components/Box'
import Popup from '@/components/Popup'
export default {
Box,
Popup
}
然后回到檔案 1 我做:
// file 1
import * as MDXComponents from './file2'
// using MDXComponents somewhere in file 1
它不允許我這樣做,但我不知道為什么?
uj5u.com熱心網友回復:
當你這樣做時:
export default {
Box,
Popup
};
您將默認匯出設定為具有 2 個屬性的新物件。您需要像這樣匯入:
import MDXComponents from './file2';
// And then destructure the object like any normal object.
// You can't do import {} in this case, you get the full object.
const { Box } = MDXComponents;
當你這樣做時:
export {
Box,
Popup
};
您創建兩個可以匯入的命名匯出,如下所示:
// import all named exports
import * as MDXComponents from './file2';
// or just one individually
import { Box } from './file2';
對于這個用例,還有一個快捷方式:
export { default as Box } from '@/components/Box';
export { default as Popup } from '@/components/Popup';
// If a module has named exports (not just a default export)
// and you want to export them all:
export * from 'some-module';
// Or just some:
export { someOtherThing } from '@/components/Box';
export { default as Popup, someOtherThing } from '@/components/Popup';
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492158.html
標籤:javascript 节点.js 反应 下一个.js mdx
