如何将 JointJS 与使用 Angular CLI 构建的应用程序一起使用?

How can I use JointJS with an application built with Angular CLI?

我已经通过 npm 安装了 jointjs 并且还安装了 typings 代码 compiles/builds 很好。

代码

import { Component } from '@angular/core';
import  * as joint from '../../node_modules/jointjs/dist/joint.min';


export class AppComponent {
  title = 'app works!';
  constructor(
  ) {
    // console.log(joint)// private jint: joint,
  }

  ngOnInit() {
    var graph = new joint.dia.Graph;
  }
}

浏览器出现错误:

Failed to compile.

/home/vinay/angularapps/jjs/src/app/app.component.ts (17,31): Property 'Graph' does not exist on type '{}'.)

我的 cli.json 文件.. 添加了 jointjs

的脚本和样式
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "version": "1.0.0-beta.32.3",
    "name": "jjs"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "../node_modules/jointjs/css/layout.css"
      ],
      "scripts": ["../node_modules/jointjs/dist/joint.js"],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

我的 tsconfig.json 文件,我在其中添加了 allowJs 到 true

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ],
    "mapRoot": "./",
    "module": "es2015",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

我无法弄清楚如何创建在此 link 上提供的简单的 hello world 应用程序 http://resources.jointjs.com/tutorial/hello-world

  1. npm install jointjs --save
  2. at angular.cli.json 插入脚本:./node_modules/jointjs/dist/jointjs.js
  3. at angular.cli.json 插入样式:./node_modules/jointjs/css/layout.css

好的,我终于让 jointjs 与@angular cli 一起工作,下面是步骤

在命令提示符中在angular项目目录中

  1. ng 新的你的应用程序
  2. cd 进入你的应用程序
  3. npm install jquery --save npm install @types/jquery --save-dev
  4. npm install backbone --save npm install @types/backbone --save-dev
  5. npm install jointjs --save npm install @types/jointjs --save-dev
  6. npm install lodash@3.10.1 --save npm install @types/lodash@3.10.1 --保存开发

确保 在 package.json 中条目显示在依赖项和 devDepencies 下 backbone, jquery, jointjs, lodash 并确保它们是

的版本

jointjs 1.0.3,backbome = 1.3.3,jquery = 3.1.1,lodash = 3.10.1

angular-cli.json文件

在样式 属性 和脚本中;[ ] 属性 添加如下所示的联合 js 详细信息

  "styles": [
    "styles.css",
    "../node_modules/jointjs/css/layout.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jointjs/dist/joint.js"
  ],

你的component.html

 <div id="paper"></div>

你的component.ts

import * as jQuery from 'jquery';
import * as _ from 'lodash';
import * as $ from 'backbone';
const joint = require('../../../../node_modules/jointjs/dist/joint.js');

ngOnInit() {
    let graph = new joint.dia.Graph;

    let paper = new joint.dia.Paper({
      el: jQuery("#paper"),
      width: 600,
      height: 200,
      model: graph,
      gridSize: 1
    });

    let rect = new joint.shapes.basic.Rect({
      position: { x: 100, y: 30 },
      size: { width: 100, height: 30 },
      attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
    });

    let rect2 = rect.clone() as joint.shapes.basic.Rect;
    rect2.translate(300);

    var link = new joint.dia.Link({
      source: { id: rect.id },
      target: { id: rect2.id }
    });

    graph.addCells([rect, rect2, link]);
}

只是提一下,在接受的答案中有一些错误或遗漏的地方:

  1. 包含在component.ts中应该是:

    declare var $:JQueryStatic;
    import * as _ from 'lodash';
    import * as backbone from 'backbone';
    import * as joint from 'jointjs';
    
  2. 您应该在 jquery 和 backbone 的 npm install 命令中添加版本,以确保使用所提到的建议版本。

  3. 您还(另外)必须将以下 2 css 个文件添加到 angular-cli.json:

    "../node_modules/jointjs/css/themes/material.css",
    "../node_modules/jointjs/css/themes/default.css"
    
  4. 在代码中使用 $ 而不是 jQuery:

    let paper = new joint.dia.Paper({
       el: $('#paper'),
       ...
     });
    

第一个,

PS:我用 angular 4.1 和 angular-cli 1.0.3

测试了它

我正在使用 @angular/cli : 1.0.4 版本,我将毫无问题地汇总 运行 JointJS 所需的一切。

使用这些命令正确安装 npm 包并确保存在正确的版本。 devdependencies 上的类型和正常依赖项上的库很重要。

npm install --save lodash@3.10.1 jquery@3.1.1 backbone jointjs
npm install --save-dev @types/lodash@3.10.1 @types/jquery @types/backbone @types/jointjs

这是我在 angular-cli.json 文件中的脚本:

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/lodash/index.js",
    "../node_modules/backbone/backbone.js",
    "../node_modules/jointjs/dist/joint.js"
  ]

这是我在 angular-cli.json 文件中正常工作的样式:

"styles": [
    "styles.scss",
    "../node_modules/jointjs/css/layout.css",
    "../node_modules/jointjs/css/themes/material.css",
    "../node_modules/jointjs/css/themes/default.css"
  ],

例子同上。 希望我能帮忙。编码愉快!