简体   繁体   English

在 TypeScript 中导入时出现“Uncaught SyntaxError: Unexpected token {”

[英]"Uncaught SyntaxError: Unexpected token {" when importing in TypeScript

Question: Why do I get "Uncaught SyntaxError: Unexpected token {" at line 2 of Game.ts file ?问题:为什么我会在 Game.ts 文件的第 2 行收到“Uncaught SyntaxError: Unexpected token {”?

I have two typescript files listed below.我有下面列出的两个打字稿文件。 I don't get any errors when compiling my project through visual studio.通过 Visual Studio 编译我的项目时,我没有收到任何错误。 However, when I look at the console in my browser (tried different ones) I get this error and my game scene is not rendered.但是,当我在浏览器中查看控制台时(尝试了不同的控制台),我收到此错误并且我的游戏场景未呈现。 I use TypeScript 3.0 and ES6.我使用 TypeScript 3.0 和 ES6。

ColorHelper.ts颜色助手.ts

export class Color {
    ColorFromRGB(r: number, g: number, b: number): BABYLON.Color3 {
        return new BABYLON.Color3(r / 255, g / 255, b / 255);
    }
}

Game.ts游戏.ts

///<reference path="../node_modules/babylonjs/babylon.d.ts" />
import { Color } from "./helpers/ColorHelper";

class Game {
    ...
}

Here is my tsconfig.json这是我的 tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "types": [ "babylonjs" ],
    "target": "es6",
    "sourceMap": true,
    "module": "es6"
  }
}

Here is my package.json这是我的 package.json

{
  "name": "BabylonTest",
  "version": "1.0.0",
  "description": "",
  "main": "Game.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babylonjs": "^3.2.0"
  }
}

When using ES6, you have to remember to add the compilerOptions module , target is ES6 but module is commonjs使用ES6的时候记得添加compilerOptions模块,target是ES6但是module是commonjs

  {  
        "compilerOptions": {
             "module": "commonjs", // add this instead of es6 as module
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM