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

时间:2017-02-19 14:17:23

标签: angular angular-cli jointjs

我已经通过npm安装了jointjs并且还安装了打字 并且代码编译/构建正常。

代码

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"
    ]
  }
}

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

3 个答案:

答案 0 :(得分:13)

好吧我终于得到了使用@angular cli的联合工作,下面是步骤

角项目目录

中的

命令提示符

  1. ng new your-app
  2. cd into your-app
  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 --save-dev的
  7. 确保 in 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 文件

    中的

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

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

    中的

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

    your 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 :(得分:4)

我正在使用@ 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"
  ],

示例与上述相同。 希望我能提供帮助。快乐的编码!

答案 2 :(得分:0)

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