如何使用带@types/jstree的打字稿将jstree添加到angular 2应用程序

How to add jstree to angular 2 application using typescript with @types/jstree

嗨,我认为这是一个新手问题,但我是 ng2 的新手,所以请多多包涵...

我完成了:

npm i jstree --save

在我使用 angular-cli 应用程序创建之后。

然后我安装了:

npm i @types/jstree --save-dev

那是在设置中,然后我尝试使用它但没有成功。

请有人告诉我如何使用带有打字稿的第 3 方库

@IamStalker,看来我知道问题出在哪里了。但是你能提供更多代码来说明你想如何使用它吗?


由于 jstree 依赖于 jQuery,因此您也必须导入它。

您可能需要使用 scripts 配置。

引用如下:https://github.com/angular/angular-cli/wiki/stories-global-scripts.

以下是我为让它发挥作用所采取的步骤。 我从一个新的 cli 应用程序开始。

npm install --save jquery jstree

npm install --save-dev @types/jquery @types/jstree

然后我更新了 angular.json 文件,如果您使用的是旧版本的 angular-cli,请对 angular-cli.json 文件进行更改。我将 "../node_modules/jstree/dist/themes/default-dark/style.min.css" 添加到样式 属性 数组中。

我还在脚本 属性 数组中添加了两项: "../node_modules/jquery/dist/jquery.min.js", "../node_modules/jstree/dist/jstree.min.js"

然后我将 src/app/app.component.html 更新为

<div id="foo">
  <ul>
    <li>Root node 1
      <ul>
        <li>Child node 1</li>
        <li><a href="#">Child node 2</a></li>
      </ul>
    </li>
  </ul>
</div>

我也将src/app/app.component.ts更新为

import { Component, OnInit } from '@angular/core';

declare var $: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit(): void {
    $('#foo').jstree();
  }
}

希望对您有所帮助!