首页 > 解决方案 > 如何在 Angular 2 应用程序中包含 jquery 库

问题描述

如何在 Angular 2 应用程序中包含第三方 jQuery 库?
如何在 Angular 2 应用程序中包含http://keith-wood.name/calendarspicker.html jQuery 库?

标签: javascriptangularangular2-forms

解决方案


1-除了通过 npm 安装之外,尝试在你的 angular-cli.json 文件中包含这一行,在 apps->scripts 键中,如下例所示:

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

2-将此插件添加到 webpack.config.js 中的 webpack 插件(在 module.exports 中):

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

3- 然后将它导入到你需要使用它的组件中,例如: import * as $ from 'jquery';


推荐阅读