我想知道如何从语法上更改这个自动执行的导出函数:
export default (() => ({
get items() {
if (process.env.NODE_ENV === 'test') {
return { message: 'this is testing' };
} else {
return { message: 'this is not testing' };
}
},
}))();转换为使用ES6箭头函数的内容和末尾的导出语句(我猜错了):
const items = (() => {
if (process.env.NODE_ENV === 'test') {
return { API_AUTHORITY: 'this is testing' };
} else {
return { API_AUTHORITY: 'this is not testing' };
}
})();
export default items;谢谢!
发布于 2018-07-18 09:54:01
不需要生命周期-只需导出对象即可:
const obj = {
get items() {
if (process.env.NODE_ENV === "test") {
return { message: "this is testing" };
} else {
return { message: "this is not testing" };
}
}
};
export default obj;如果你真的想要一个函数,那么定义它一行,然后你可以在导出的时候执行它,尽管意图不是很清楚:
const fn = () => ({
get items() {
if (process.env.NODE_ENV === "test") {
return { message: "this is testing" };
} else {
return { message: "this is not testing" };
}
}
});
export default fn();或
const fn = // same as above
// ...
const obj = fn();
export default obj;发布于 2018-07-18 10:07:11
@Li357在评论中提供了答案,这个函数是不必要的:
export default { items: { message: process.env.NODE_ENV === 'test' ? 'this is testing' : 'this is not testing' } }https://stackoverflow.com/questions/51392350
复制相似问题