[英]Use jQuery in a custom Angular 6+ library
EDIT2编辑2
I get the feeling my approach to write an independent angular library for others to use is wrong?我觉得我编写一个独立的 angular 库供其他人使用的方法是错误的?
My first thought was writing a npm module but it has to be private and cant be published - would that work / be easy?我的第一个想法是编写一个 npm 模块,但它必须是私有的并且不能发布 - 这会工作/容易吗?
Is my approach wrong?我的方法错了吗?
How do I use jQuery in my Angular 6+ library?如何在我的 Angular 6+ 库中使用 jQuery?
I created a library with我创建了一个库
ng generate library nodeeditor --prefix=lib
Then added a jQuery npm module to it with然后向其中添加了一个 jQuery npm 模块
cd projects/nodeeditor
npm i --save jquery
But when stating但是当陈述
import * as jQuery from 'jquery/dist/jquery.min.js'
in nodeeditor.component.ts (as I did in Angular projects only when absoloutely necessary)在 nodeeditor.component.ts 中(就像我在 Angular 项目中所做的那样,仅在绝对必要时)
ng build nodeeditor
gives me给我
BUILD ERROR Cannot call a namespace ('jQuery')
构建错误无法调用命名空间('jQuery')
Anyone got any idea?有人知道吗?
EDIT1编辑1
Generated folder structure looks like this生成的文件夹结构如下所示
npm install jquery --save
.angular-cli.json or angular.json: .angular-cli.json 或 angular.json:
"scripts": [
"node_modules/jquery/dist/jquery.js"
]
Import jquery file module.ts file导入jquery文件module.ts文件
import * as $ from 'jquery';
to check jquery included in our application write $ in the browser console要检查我们的应用程序中包含的 jquery,请在浏览器控制台中写入 $
您应该按如下方式更改导入:
import * as jQuery from 'jquery';
I hope I've a way to do this.我希望我有办法做到这一点。 I know this is a bit late.
我知道这有点晚了。 But it might help some one in need.
但它可能会帮助有需要的人。 I'm able to use the jQuery in my library.
我可以在我的库中使用 jQuery。 Here most of the answers cover how to install jquery / @types in the parent application.
这里大部分答案涵盖了如何在父应用程序中安装 jquery/@types。 That is part of the answer but not all.
这是答案的一部分,但不是全部。
Since angular library implementation expect the node modules to be delivered by the parent application, we need to install jQuery / @types at the parent app level and add that to scripts section of the angular.json like mentioned in the most of the answers.由于 angular 库实现期望节点模块由父应用程序交付,因此我们需要在父应用程序级别安装 jQuery / @types 并将其添加到 angular.json 的脚本部分,就像大多数答案中提到的那样。
How to make it work in library then?那么如何让它在图书馆工作呢?
How I approached this is我是如何处理这个的
Ref:参考:
package.json包.json
{
"name": "testing-lib",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^10.0.9",
"@angular/core": "^10.0.9",
"bootstrap": "^4.4.1",
"jquery": "^3.5.1",
"@types/jquery": "^3.5.1",
"ngx-toastr": "13.2.1",
"@angular/material": "^9.0.0"
},
"dependencies": {
"tslib": "^2.0.0"
},
"devDependencies": {
"ckeditor4-angular": "^2.1.0",
"jquery": "^3.5.1",
"bootstrap": "^4.4.1",
"ngx-toastr": "13.2.1",
"@types/jquery": "^3.5.1",
"@angular/material": "^9.0.0"
}
}
ng-package.json ng-package.json
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/testing-lib",
"lib": {
"entryFile": "src/public-api.ts",
"umdModuleIds": {
"ckeditor4-angular": "ckeditor4-angular",
"ngx-toastr": "ngx-toastr",
"bootstrap": "bootstrap",
"jquery": "jquery",
"@angular/material": "@angular/material"
},
"styleIncludePaths": [
"node_modules/bootstrap/dist/css/bootstrap.min.css"
]
}
}
tsconfig.lib.json tsconfig.lib.json
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [
"jquery"
],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
After this I build my library and the jQuery started to work.在此之后,我构建了我的库并且 jQuery 开始工作。
Note: Please make sure your parent app does have jQuery and @types installed.注意:请确保您的父应用程序确实安装了 jQuery 和 @types。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.