将打字稿库添加到 angular

Add a typescript library to angular

我安装了一个名为 Jodit 的文本编辑器,但在尝试将其集成到我的 angular 应用程序时遇到了一些问题。
按顺序,我做了:

  1. npm install --save jodit
  2. 添加到 angular.json build-options-sripts "node_modules/jodit/build/jodit.min.js"
  3. 已添加到 angular.json 构建选项样式 "node_modules/jodit/build/jodit.min.css"

这是我的组件:

import * as jodit from 'jodit';

@Component({
  selector: 'app-test',
  styleUrls: ['./test.component.scss'],
  template: `
    <textarea id="test" name="editor"></textarea>
  `
})
export class TestComponent implements OnInit {
  public editor: any;

  constructor() {}

  ngOnInit() {//
    const elem: HTMLElement = document.getElementById('test');
    this.editor = new jodit.Jodit(elem, {});
}

我收到以下错误

src/app/test.component.ts(21,28): error TS2339: Property 'Jodit' does not exist on type 'typeof import("C:/Users/test/Desktop/test/node_modules/jodit/src/types/index")'.
src/app/test.component.ts(41,27): error TS2339: Property 'Jodit' does not exist on type 'typeof import("C:/Users/test/Desktop/test/node_modules/jodit/src/types/index")'.

我无法编译它,但是 npm start 我可以让它工作(我仍然有错误,但它编译了,我可以看到文本编辑器)。
我是不是遗漏了什么,看起来像是类型链接错误?

我尝试创建 angular 模块,但现在您可以像这样使用它 在你的 angular.json

"scripts": [
  "../node_modules/jodit/build/jodit.min.js",
],

typings.d.ts中添加

declare var Jodit: any;

并像这样创建组件

import {
  Component,
  OnDestroy,
  AfterViewInit,
  EventEmitter,
  Input,
  Output
} from '@angular/core';

@Component({
  selector: 'simple-jodit',
  template: `<textarea id="{{elementId}}"></textarea>`
})
export class SimpleJoditComponent implements AfterViewInit, OnDestroy {
  @Input() elementId: String;
  @Output() onEditorKeyup = new EventEmitter<any>();

  editor;

  ngAfterViewInit() {
    this.editor = new Jodit('#' + this.elementId, {});
  }

  ngOnDestroy() {
   this.editor.destruct();
  }
}

并使用它

<simple-jodit
  [elementId]="'my-editor-id'"
  (onEditorKeyup)="keyupHandlerFunction($event)"
  >
</simple-jodit>

您也可以下载这个包装器Jodi component wrapper