[英]Exporting imports using module.exports and ES6
我正在尝试将一个函数导入一个文件,然后从该文件中导出它。 这应该很简单,但由于某种原因我无法让它工作。
search_action.js
function search_actions() {
this.receive_results = function() {
return {
type: 'RECEIVE_RESULTS',
results: results
}
}
}
module.exports = search_actions
索引.js
require('es6-promise').polyfill();
var SearchActions = require('./search_actions.js')
var search_actions = new SearchActions()
//console.log(search_actions.receive_results)
export search_actions.receive_results
尽管 console.log(search_actions.receive_results) 打印了该函数,但 index.js 底部的导出失败并带有意外标记。 那么这样做的正确方法是什么?
重新导出的最后一行无效:
export search_actions.receive_results
您不能在右侧使用foo.bar
引用,因为导出需要一个不合格的名称。 您可以在对象声明中引用该字段并导出:
export default {
search_actions: search_actions.receive_results
}
有关导出语法,请参阅规范的第 15.2.3 节。 您遇到的问题是导出的xy
部分,对象或局部变量将解决该问题。
如果您也使用 ES6 import
,您还可以执行以下操作:
import {receive_results} from 'search_actions';
export default receive_results;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.