Angular 4 - 如何正确导入fancytree

Angular 4 - how to correctly import fancytree

我试图将 fancytree 导入我的 Angular 4 项目。 我按照这个指令做了一切: https://github.com/mar10/fancytree/wiki/TutorialIntegration#howto-run-fancytree-with-angular-4

但最后每次都报错

$(...).fancytree is not a function

此说明中是否缺少某些内容...?

到目前为止我做了什么:

  1. npm install --save jquery jquery.fancytree
  2. 已将脚本和样式添加到 angular-cli.json:
[...]
"styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
        "../node_modules/jquery.fancytree/dist/skin-win8/ui.fancytree.min.css",
        "styles.scss"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "../node_modules/jquery.fancytree/dist/jquery.fancytree-all-deps.min.js"
      ],
[...]
  1. 在 tsconfig.app.json 中添加的类型:
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": ["jquery","jquery.fancytree"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}
  1. 在组件的 html:
  2. 中添加树 id
<div id="tree">

</div>
  1. 尝试在组件中使用:
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
// const fancytree = require('jquery.fancytree'); // doesn't work
import * as $ from 'jquery';

@Component({
  selector: 'table-of-content',
  templateUrl: './table-of-content.component.html',
  styleUrls: ['./table-of-content.component.scss']
})
export class TableOfContentComponent implements OnInit {

  @Output() isTocClosed: EventEmitter<boolean> = new EventEmitter<boolean>();

  constructor() { }

  ngOnInit() {
    $('#tree').fancytree({
      extensions: ['edit', 'filter'],
      source: [
        { title: "Node 1", key: "1" },
        {
          title: "Folder 2", key: "2", folder: true, children: [
            { title: "Node 2.1", key: "3" },
            { title: "Node 2.2", key: "4" }
          ]
        }
      ]
    });

    // const tree = fancytree.getTree('#tree');
  }

}

好的,找到了! 组件中确实需要那些:

import 'jquery.fancytree/dist/modules/jquery.fancytree.edit';
import 'jquery.fancytree/dist/modules/jquery.fancytree.filter';

或者如果有人喜欢:

require('jquery.fancytree/dist/modules/jquery.fancytree.edit');
require('jquery.fancytree/dist/modules/jquery.fancytree.filter');

并在我们需要的组件中使用 fancytree 对象:

const fancytree = require('jquery.fancytree');