我正在尝试导出ES6中的函数,以便从其他文件中访问它们。但我不知道怎么做。
文件1:(导入)
import components from './components/components';
console.log(components.hej);档案2:(出口)
var hej = () => {
console.log('HEj');
};
export var hej;为什么我不能从文件1访问文件2中声明的函数"hej“?这对我来说毫无意义。
请帮帮我!
发布于 2016-05-18 15:31:07
您正在执行一个指定的导出,而不是默认的导出,因此导入语法将无法工作。要导入现有的hej,您必须这样做:
// Imports a single object by name
import { hej } from './components/components';
console.log(hej);或者:
// Imports all exported objects grouped together under the specified name
import * as components from './components/components';
console.log(components.hej);而且,您的导出语法不正确-- export var hej应该是export { hej },因为您没有在那里定义一个新变量,而是使用现有的变量。或者,您可以将函数声明更改为export var hej = () => { ... };,这将产生同样的效果。
https://stackoverflow.com/questions/37304004
复制相似问题