Angular 4-如何正确导入fancytree

时间:2019-07-15 13:11:13

标签: angular fancytree

我试图将fancytree导入到我的Angular 4项目中。 我按照以下说明进行了所有操作: https://github.com/mar10/fancytree/wiki/TutorialIntegration#howto-run-fancytree-with-angular-4

但是最后每次遇到错误

  

$(...)。fancytree不是函数

该指令中是否缺少某些内容??

到目前为止我所做的事情:

  1. npm install-保存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中添加了树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');
  }

}

1 个答案:

答案 0 :(得分:1)

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

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');