导入目录的索引文件无法正常工作
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"); }
目录结构的图片如下所示:
你不会输出任何名为home
东西。 如果你想把你输出的所有东西都附加到一个名为home
的variables上,那么使用as
。
import * as home from './routes/index';
有关导入/导出的更多信息,请参阅此处 。
你可以像这样构造你的代码来达到你想要的效果:
的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 };