首页 > 解决方案 > 在自定义 Angular 6+ 库中使用 jQuery

问题描述

EDIT2
我觉得我编写一个独立的角度库供其他人使用的方法是错误的?
我的第一个想法是编写一个 npm 模块,但它必须是私有的并且不能发布 - 这会工作/容易吗?
我的方法错了吗?

如何在我的 Angular 6+ 库中使用 jQuery?

我创建了一个图书馆

ng generate library nodeeditor --prefix=lib

然后添加了一个 jQuery npm 模块到它

cd projects/nodeeditor    
npm i --save jquery

但是当说明

import * as jQuery from 'jquery/dist/jquery.min.js'

在 nodeeditor.component.ts 中(就像我在 Angular 项目中所做的那样,仅在绝对必要时)

ng build nodeeditor

给我

构建错误无法调用命名空间('jQuery')

有人知道吗?

EDIT1
生成的文件夹结构如下所示

角度库文件夹结构

标签: jqueryangular

解决方案


npm install jquery --save

.angular-cli.json or angular.json:

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

Import jquery file module.ts file

import * as $ from 'jquery';

to check jquery included in our application write $ in the browser console


推荐阅读