没有名称的“导出默认{}”是什么意思

Kat*_*ate 4 react-native expo

我发现 React Native 模块的代码如下:

export default {
    activateWithApiKey(apiKey: string) {
        AppMetrica.activateWithApiKey(apiKey);
    },
};
Run Code Online (Sandbox Code Playgroud)

奇怪的是“导出默认”没有名称。谷歌搜索中的所有示例都有“导出默认 SomeName”。有人知道没有名字的“导出默认”是什么意思吗?谢谢你的帮助。

Nym*_*ine 7

参考默认导出语法

\n
// Default exports\nexport default expression;\nexport default function (\xe2\x80\xa6) { \xe2\x80\xa6 } // also class, function*\nexport default function name1(\xe2\x80\xa6) { \xe2\x80\xa6 } // also class, function*\nexport { name1 as default, \xe2\x80\xa6 };\n
Run Code Online (Sandbox Code Playgroud)\n

正如您所看到的,name可以指定为函数、类、函数*(例如export default class Component)。

\n

当您在当前模块中进一步需要导出的函数时,通常使用此选项。请注意,如果为 指定了名称export default,则在导入过程中仍然可以使用其他名称。\n例如,函数被导出并可以在当前模块中进一步使用:

\n
// module-a.js\nexport default function originalName() { console.log(\'i am default\') };\n\noriginalName();\n\n// module-b.js\nimport someAnotherName from \'./module-a.js\'\n
Run Code Online (Sandbox Code Playgroud)\n

回答你的问题

\n
\n

有人知道没有名字的“导出默认”是什么意思吗?

\n
\n

export default expression;意味着expression名称可能在导出阶段丢失:

\n
// module-a.js\nexport default {\n    activateWithApiKey(apiKey: string) {\n        AppMetrica.activateWithApiKey(apiKey);\n    },\n};\n\n// module-b.js\nimport anyName from \'./module-a.js\' \n// creates local variable `anyName` and assigns object from \'module-a.js` to it\n
Run Code Online (Sandbox Code Playgroud)\n