Ich habe eine JSON-Datei, die ich in meine Angular 2-Anwendung importieren muss. Ich habe Schritte aus diesem How to Import json into TypeScript Artikel gefolgt, aber ich bekomme einen "SyntaxError: Unexpected token :
" Fehler. Was denkst du, mache ich falsch?Fehler beim Importieren der JSON-Datei
Hier ist mein Code:
Voll Fehler:
(index):28 Error: SyntaxError: Unexpected token :
at eval (<anonymous>)
at Object.eval (http://localhost:37967/Application/Settings.js:3:19)
at eval (http://localhost:37967/Application/Settings.js:92:4)
at eval (http://localhost:37967/Application/Settings.js:93:3)
at eval (<anonymous>)
Evaluating http://localhost:37967/appSettings.json
Evaluating http://localhost:37967/Application/Settings.js
Evaluating http://localhost:37967/Application/BaseComponent.js
Evaluating http://localhost:37967/Application/Components/Home/Home.js
Evaluating http://localhost:37967/Application/Module.js
Evaluating http://localhost:37967/Application/Startup.js
Error loading http://localhost:37967/Application/Startup.js
Settings.ts:
/// <reference path="../scripts/typescriptdefinitions/typings.d.ts" />
..
import * as appSettings from '../../appSettings.json';
..
public static get AppSettings(): any{
return <any>appSettings;
}
..
typings.d.ts:
declare module "*.json" {
const value: any;
export default value;
}
appSettings.json:
{
"AppSettings": {
"Url": ...
}
}
ich den Netzwerk-Panel und die Datei mit 200 OK
geladen wird überprüft haben. Auch die json Inhalte sind gültig json.
Frage Frage: FMY könnte mir bitte erklären, den Zweck des Importierens von JSON-Datei vs Laden mit http.request()? – Vega
@Vega: Laden mit http ist asynchron. Die JSON-Datei enthält URL für eine API und ich habe API-Aufrufe auf der Hauptseite und ich hatte Fehler, weil die Aufrufe auf der Hauptseite, die vor der JSON-HTTP-Anfrage ausgeführt wurden, abgeschlossen wurden. Import ist Sync, damit wird dieses Problem behoben. – dstr
Großartige Erklärung, danke! – Vega