当前位置:  开发笔记 > 编程语言 > 正文

jQuery没有为angular 2定义

如何解决《jQuery没有为angular2定义》经验,为你挑选了2个好方法。

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

jQuery没有定义

我已经尝试运行以下npm安装但它没有解决任何问题.

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

我的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"
  }
}

我的文件夹结构



1> Kacper Polak..:

你需要导入jquery angular-cli.编辑您的angular-cli.json文件.

查找script数组并添加jquery.

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



2> Anand Rockzz..:

浪费了大量的时间,这是我做的,没有帮助.

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

和所有其他常见的事情,

清除浏览器缓存

重新启动浏览器

杀死并重新运行服务


在我的情况下,我必须做以下事情:

npm install --save @types/jquery

在.angular-cli.json中导入jquery

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

改变代码

import $ from 'jquery'; 

import * as jquery from 'jquery';

注意:角度从4.2升级到5.2后开始出现问题

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有