【问题标题】:Importing jQuery plugin 'jquery.shorten' in TypeScript/Angular 4在 TypeScript/Angular 4 中导入 jQuery 插件“jquery.shorten”
【发布时间】:2018-01-30 08:49:25
【问题描述】:

我想在使用 npm 的 Ionic 项目中使用名为 jquery.shorten 的 jQuery 插件。

安装命令是npm install jquery.shorten --save

我已经成功地包含了jQuery,我可以在我的项目中使用它。但无法使用jquery.shorten

以下是我用来包含jQueryjquery.shorten 的代码。它在角度项目的app.module.ts -

import * as $ from 'jquery';
import 'jquery.shorten';

我在控制台看到的错误是 -

未捕获的错误:找不到模块“jquery.shorten”

我也尝试过关注。这次所有控制台错误都消失了。但我找不到使用它的方法。

import * as shortMe from 'jquery.shorten';

它应该用作$(".dynamic-multiple-elements").shorten()

我关注了几个instructions 来包含它。但似乎没有任何效果。 我可以在node_modules 中看到带有所需js 文件的文件夹jquery.shorten

我将无法使用this solution,因为我的元素是动态的并且很多。

【问题讨论】:

  • 导入'jquery.shorten';将查看 package.json 并尝试加载使用“main”指定的文件。在 jquery.shorten 的情况下,它设置为 ''main": "jquery.shorten.js"。不幸的是,根级别没有这样的文件。所以我想你必须通过 import 'jquery.shorten/src/jquery.shorten'; 导入它除此之外,我根本不建议在 Angular 中使用 jQuery。
  • 使用 jQuery 和 Angular4 的原​​因是什么?
  • @Sebastian,我正在做一个 Ionic 项目。我尝试使用类似于您建议的特定路径导入jquery.shorten。我目前收到错误消息"jQuery" is not defined,即使我已经导入它并包含它的绑定。我会检查不同的导入方式。
  • @Stwosch,jQuery 在我的项目中很少使用(准确地说是 2 行)。它比 Angular 更容易定位。但我将使用两个插件,jquery.shorten(用于折叠大消息)和enjoyhint(用于用户指南)。我找不到上述两个的任何替代解决方案,这就是为什么我需要在我的项目中包含 jQuery。
  • 什么时候说jQuery" is not defined?在编译期间或应用程序启动时?

标签: jquery angular typescript


【解决方案1】:

您可以查看here 如何在 Angular 中使用 JQuery 和其他第三方全局变量。

没有InjectionToken的简单方法,你可以这样做:

npm install jquery --save.

npm install jquery.shorten --save.

在 angular-cli.json 中添加两个脚本引用

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

如果你不使用 angular-cli,请将脚本放入 webpack。

在任何组件中使用:

declare let $ : any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

   ngOnInit(): void {
      console.log($);
      $('#text').shorten();    
   }
}

要测试的组件 html

<p id="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>

【讨论】:

  • 哦,我之前对这个答案很感兴趣。第一次对我来说它看起来很复杂。我正在寻找一种以最简单的方式包含它的方法。我目前正在查看this solution(这是一种黑客攻击),但我会以最佳解决方案回来。
  • 好的,我这里用简单的方法模拟,效果很好。我会更新答案。
  • 您的解决方案看起来很有希望(我还没有测试过)。但我想要更好的解决方案,它不需要非常具体的路径。看看我发布的答案。如果我们可以消除编辑webpack.config.js 的必要性会更好。 @Sebastian 提出了一个很好的解决方案。但是我在控制台中遇到与未定义的 jQuery 相关的错误。
【解决方案2】:

好的,这是我已经实现的 (using this),它运行良好。

1. app.module.ts - 这样我的导入在所有组件中都可用。

import 'jquery';
import 'jquery.shorten/src/jquery.shorten';

2.我将以下代码添加到webpack.config.js -

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

3.home.ts中声明变量declare let $: any;declare let jQuery: any;,我想同时使用jQueryjquery.shorten

问题 - 文件 webpack.config.js 存在于 node-modules 文件夹下。在传输代码或重新安装 npm 时很有可能会被替换。

期望 - 即使项目文件被传输,更改也应该保留。

如果可能的话,我想要类似于this solution 的解决方案。

【讨论】:

  • 我想在没有任何解决方法的情况下解决这个问题。 @Sebastian 提供了几乎类似的解决方案。但由于某种原因,我们收到了jQuery is not defined 的错误。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2017-10-11
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
相关资源
最近更新 更多