【问题标题】:exports is not defined in Typescript出口未在 Typescript 中定义
【发布时间】:2018-01-12 15:02:15
【问题描述】:

我在 Typescript 中添加了 JQuery,但出现了这个错误:

未捕获的引用错误:未定义导出

代码 TS:

import * as $ from 'jquery'

  $('.test').click(function(eventObject){
      alert('med');
  })  

代码js:

"use strict";
 exports.__esModule = true;
var $ = require("jquery");
$('.test').click(function (eventObject) {
    alert('med');
});

tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "es6",
        "pretty": true,
        "experimentalDecorators":true
    },

    "files": [
        "demo.ts",
        "typings/index.d.ts"
    ],

    "excluse": ["node_modules"]
}

如何解决这个错误?

【问题讨论】:

  • 你能发布你的 tsconfig 文件吗?
  • 你从哪里得到这个错误?
  • 您是否安装了@types/jqueryjquery npm 包?
  • @rgvassar ,我在添加代码 tsconfig 时更新了我的问题
  • @unional ,我在控制台中有这个错误(在代码 js 中)

标签: jquery typescript


【解决方案1】:

你试过...

declare var $ :any


  $('.test').click(function(eventObject){
      alert('med');
  })  

【讨论】:

  • 错误 TS2451:无法重新声明块范围变量“$”。
  • @devit2017 Frederico 的意思是使用这个代替您的import 语句,并将jQuery 包含在您的全局脚本中,以便在window 上可用。我假设(这是一种常见的模式)。
  • @devit2017 鉴于您的错误消息,我只是在扩展答案。这不是我解决问题的方式,购买我没有立即解决方案,所以我没有写答案。我还在一个 ng 4 应用程序中使用 jQuery 与 Angular cli...
  • @msanford,我添加了 declare var $ :any 但我有这个错误:错误 TS2451:无法重新声明块范围变量 '$'
  • 你能把你所有的课都发上来吗?
猜你喜欢
  • 2017-09-14
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 2018-02-28
相关资源
最近更新 更多