2016-04-04 7 views
1

Der folgende Inhalt der Datei application.js meldet, dass "home", das importiert wird, nicht definiert ist.Importieren der Indexdatei für das Verzeichnis, das nicht ordnungsgemäß funktioniert

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

Der Gehalt an index.js sind wie folgt:

export * from "./home.js" 

Der Gehalt an home.js sind wie folgt:

export const type = "GET" 

export const route = "/" 

export const middleware = []; 

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

Ein Bild von der Verzeichnisstruktur ist wie folgt :

directory

+0

Ihre Exporte werden nicht von 'home' vorangestellt. Versuchen Sie 'import {middleware} von './routes'' (kein Index). Das gleiche gilt für "type", "route" und "action" in Ihrem Beispiel. –

Antwort

1

Sie exportieren nichts mit dem Namen home. Wenn Sie alles anhängen möchten sind Sie Exportieren in eine Variable mit dem Namen home dann verwenden Sie as.

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

Siehe here for more information on import/export.

+0

Ich habe das versucht und habe den Fehler bekommen: 'SyntaxError: src/routes/index.js: Unerwartetes Token (1: 9) export * als home von" ./home.js "' – Connorelsea

+0

@Connorelsea 'import', nicht "exportieren". Dies sollte 'import {home} von" ./routes/index "' ersetzen. –

+0

Ziel ist es jedoch, eine Indexdatei für das Verzeichnis zu erstellen, das den Zugriff auf alle Dateien im Verzeichnis erlaubt. So: http://stackoverflow.com/questions/32914796/how-do-i-create-a-main-import-file-in-es6 – Connorelsea

1

Sie könnten Ihren Code strukturieren wie so die Wirkung zu erzielen, die Sie wollen:

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 
}; 
Verwandte Themen