简体   繁体   English

如何导出es6函数

[英]How to export es6 function

I am creating an npm module with a load of self made functions that I can then import into my other projects. 我正在创建带有大量自制函数的npm模块,然后可以将其导入到其他项目中。 It was working until I did some refactoring and now cant work out why. 在我进行一些重构之前,它一直在起作用,现在无法弄清原因。

This is my shuffle func declaration and then exporting 这是我的shuffle func声明,然后导出

const rhShuffle = function(array) {

}

module.exports = {
  rhShuffle
}

And this is me importing it and then exporting it again so it is available outside the project. 这是我导入它,然后再次导出它,以便它在项目外部可用。 this is my index.js file at route level. 这是我在路由级别的index.js文件。 please not that when i declared the function straight in here and exported it, it worked fine 请不要当我在这里直接声明该函数并将其导出时,它工作正常

import rhShuffle from './src/shuffle';

module.exports = {
  rhShuffle
}

then in my other project I have imported it and console logged it and it says: 然后在我的另一个项目中,我已将其导入并通过控制台记录了该日志,并显示:

_richsHelpers2.default.rhShuffle is not a function

and when i console log: 当我控制台日志:

Object {rhShuffle: Object} "rh"

it says its an object 它说它是一个对象

Option 1: src/shuffle.js 选项1: src / shuffle.js

const rhShuffle = function(array) {

}
module.exports = {
  rhShuffle,
}

Usage 用法

var rhShuffle = require('./src/shuffle').rhShuffle;

Option 2: src/shuffle.js 选项2: src / shuffle.js

const rhShuffle = function(array) {

}
export default rhShuffle

Usage 用法

import rhShuffle from './src/shuffle';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM