如何在TypeScript 1.8中导入jQuery npm模块

首先,我正在努力提出正确的问题.我觉得我错过或混淆了有关TypeScript编译器,新ES2015(ES6)模块语法和模块加载器的一些概念.

我正在尝试使用ES2015模块语法导入npm安装的jquery模块.打字稿应该转发给ES5.

打开index.html时出错.

system-polyfills.src.js:1276 Error: SyntaxError: Unexpected token <
Evaluating http://localhost:3000/jquery
Error loading http://localhost:3000/app/main.js

的index.html

<html>
  <head>
    <title>Bare TypeScript</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <link rel="stylesheet" href="styles.css">
    <!-- 1. Load libraries -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/jquery/dist/jquery.js"></script>
    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>
  <!-- 3. Display the application -->
  <body>
    <div>Loading...</div>
  </body>
</html>

>我不确定是否 SRC = “node_modules / jquery的/ DIST /的jquery.js” >< /脚本>有必要的.
>当注释console.log($)时,B导入如何工作正常
出来吗? jQuery导入也应该工作吗?
>也许将npm的commonjs模块与systemjs混合是问题?

main.ts

import * as $from 'jquery'
import B from './app.b'

console.log("Monkey");
let b = new B();
console.log($);

如何配置tsc以使用ES2015模块语法?
使用TypeScript 1.8.

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

的package.json

"dependencies": {
    "es6-shim": "^0.35.0",
    "jquery": "^2.2.2",
    "systemjs": "0.19.25"
  },

typings.json

{
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
    "jquery": "registry:dt/jquery#1.10.0+20160316155526"
  }
}

最佳答案 您正在尝试将jQuery作为systemjs模块加载,但它是AMD.

此外,除非您的app.b上有默认导出,否则typescript编译器将抛出错误.您是否检查过是否出现编译错误?

这个Github对使用ES6导入语法加载jQuery有一些了解.

点赞