简体   繁体   English

如何在Angular 2应用程序中包含jQuery库

[英]how to include jquery library in angular 2 application

How to include third party jQuery library in Angular 2 application? 如何在Angular 2应用程序中包含第三方jQuery库?
How to include http://keith-wood.name/calendarspicker.html jQuery library in angular 2 application? 如何在angular 2应用程序中包含http://keith-wood.name/calendarspicker.html jQuery库?

1-In addition to install it through npm , try to include this line in your angular-cli.json file, inside apps->scripts key like this example: 1-除了要通过npm进行安装外,还应尝试将此行包含在apps-> scripts密钥内的angular-cli.json文件中,如下例所示:

{
  "apps": {
    "scripts": [
      "../node_modules/jquery/dist/jquery.min.js"
    ]
  }
}

2-Add this plugin to your webpack plugins in webpack.config.js (in module.exports): 2-将此插件添加到webpack.config.js(在module.exports中)的webpack插件中:

   new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery"
   })

3- Then import it in the component you need to use it, for example: import * as $ from 'jquery'; 3-然后将其导入您需要使用的组件中,例如: import * as $ from 'jquery';

To add an external library to an Angular 2 app you download the files and place them in your assets folder (or any folder you may chose to add) angular/src/assets/<your-library> then add the path to the library in your angular-cli.json file. 要将外部库添加到Angular 2应用程序中,请下载文件并将它们放置在您的Assets文件夹(或您可能选择添加的任何文件夹)中angular/src/assets/<your-library>然后在您的angular-cli.json文件。 If you upgrade to angular 6 the file is renamed angular.json 如果升级到angular 6,文件将重命名为angular.json

Here is an example of one of my angular-cli.json files 这是我的angular-cli.json文件之一的示例

 "styles": [
    "../node_modules/materialize-css/dist/css/materialize.css",
    "../node_modules/font-awesome/css/font-awesome.min.css",
    "styles.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/jqueryui/jquery-ui.js",
    "../node_modules/materialize-css/dist/js/materialize.js",
    "./assets/RTCMultiConnection.min.js",
    "../node_modules/hammerjs/hammer.js",
    "./assets/popup.js"
  ],

It may, however be easier to add an "angular ready" alternative like angular-bootstrap-calendar 但是,添加“ angular ready”替代方案(例如angular-bootstrap-calendar)可能会更容易

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

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