导入目录无法正常运行的索引文件

时间:2016-04-04 15:55:11

标签: javascript node.js express module ecmascript-6

application.js文件的以下内容报告未导入“home”。

import {home} from "./routes/index"
console.log(JSON.stringify(home, null, 4))

index.js的内容如下:

export * from "./home.js"

home.js的内容如下:

export const type = "GET"

export const route = "/"

export const middleware = [];

export const action = function(req, res) {
    res.send("Testing custom routes");
}

目录结构的图片如下:

directory

2 个答案:

答案 0 :(得分:1)

您未导出任何名为home的内容。如果要附加 导出到名为home的变量的所有内容,请使用as

import * as home from './routes/index';

请参阅here for more information on import/export

答案 1 :(得分:1)

您可以像这样构建代码以达到您想要的效果:

<强>的application.js

import { home } from './routes';

<强> home.js

const type = "GET"
const route = "/"
const middleware = [];
const action = function(req, res) {
    res.send("Testing custom routes");
}

export default {
    type,
    route,
    middleware,
    action
};

<强> index.js

import * as home from './home.js';
export default {
    home
};