Javascript jQuery 不是为 angular 2 定义的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41801754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:38:44  来源:igfitidea点击:

jQuery is not defined for angular 2

javascriptjquerynode.jsangular

提问by user172902

I am trying to use ng2-datetime. However, when I follow the instruction and installed npm install --save ng2-datetime, I get the following error.

我正在尝试使用 ng2-datetime。但是,当我按照说明安装 npm install --save ng2-datetime 时,出现以下错误。

jQuery not defined

jQuery 未定义

I have tried running the following npm install but it doesnt fix anything.

我试过运行以下 npm install 但它没有解决任何问题。

 npm install jquery --save 
 npm install @types/jquery --save-dev

My package.json file

我的 package.json 文件

  {
  "name": "timesheet-web-app",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.18",
    "angularfire2": "^2.0.0-beta.7",
    "bootstrap": "^4.0.0-alpha.6",
    "bootstrap-datepicker": "^1.6.4",
    "core-js": "^2.4.1",
    "firebase": "^3.6.6",
    "jquery": "^3.1.1",
    "ng2-datetime": "^1.2.2",
    "ng2-datetime-picker": "^0.12.7",
    "ng2-file-upload": "^1.2.0",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/jquery": "^2.0.39",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.24",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.0.2",
    "typescript": "~2.0.3"
  }
}

My folder structure

我的文件夹结构

回答by Kacper Polak

You need to import jquery with angular-cli. Edit your angular-cli.jsonfile.

您需要使用angular-cli. 编辑您的angular-cli.json文件。

Find scriptarray and add jquery.

查找script数组并添加 jquery。

"scripts": [
     "../node_modules/jquery/dist/jquery.min.js"
],

回答by Anand Rockzz

Wasted a good amount of time, here is what I did and didn'thelp.

浪费了很多时间,这就是我所做但没有帮助的事情

npm uninstall -g angular-cli
npm uninstall -g @angular/cli
npm cache clear --force
npm cache clean
npm install -g @angular/cli

and all other usual things,

以及所有其他常见的事情,

  • clear browser cache
  • restart browser
  • kill and re-run ng serve
  • 清除浏览器缓存
  • 重新启动浏览器
  • 杀死并重新运行 ng serve


In my case I had to do the following:

就我而言,我必须执行以下操作:

npm install --save @types/jquery

Import jquery in the .angular-cli.json

在 .angular-cli.json 中导入 jquery

"scripts": [
     "../node_modules/jquery/dist/jquery.js"
],

change code from

更改代码

import $ from 'jquery'; 

to

import * as jquery from 'jquery';

note:issue started after angular upgrade from 4.2 to 5.2

注意:问题从 4.2 升级到 5.2 后开始

回答by alecellis1985

import * as $ from 'jquery';

import * as $ from 'jquery';

window['$'] = window['jQuery'] = $;

window['$'] = window['jQuery'] = $;

The sencond line will fix your issue!

第二行将解决您的问题!