嗨,我刚才转到ECMAScript-6 javascript语法,我很喜欢它!有一件事我注意到,但没有找到一个明确的答案是使用嵌套的析构语法的导入。我的意思是这样的..。
假设我有一个像这样的文件。
export const SomeUtils = _.bindAll({ //lodash _
someFunc1(params){
// .... stuff here
},
someFunc2(params){
// .... stuff here
},
someFunc3(params){
// .... stuff here
}
});
// ... many more of these我一直在做这样的事情来获得一个特定的功能
import {Utils} from '../some/path/to/utils';
var {someFunc2} = Utils;说到重点..。有办法为someFunc2做单行导入吗?比如如何使用括号进行嵌套对象销毁分配?(又名:{Utils: [{someFunc2}]})
我以前经常做var someFunc2 = require('../some/path/to/utils').someFunc2;,但我似乎不知道如何用导入语句来实现它
发布于 2015-12-12 22:29:11
不,ES6模块导入不提供析构选项。它们所拥有的唯一特性是导出(但不嵌套)。它们正好取代了您的require(…).someFunc2模式。
在您的具体情况下,我看不出有什么理由要将单个对象导出为指定的导出。只管用
export function someFunc1(params){
// .... stuff here
}
export function someFunc2(params){
// .... stuff here
}
export function someFunc3(params){
// .... stuff here
}这样你就可以
import {someFunc2} from '../some/path/to/utils';发布于 2015-12-12 22:35:23
要实现您想要的结果,您需要将其导出为默认值:
const Utils = _.bindAll({ //lodash _
someFunc1(params){
// .... stuff here
},
someFunc2(params){
// .... stuff here
},
someFunc3(params){
// .... stuff here
}
});
export default Utils;然后你可以导入你所需要的全部东西.
import Utils, { someFunc2 } from '../some/path/to/utils';https://stackoverflow.com/questions/34245546
复制相似问题