joint.shapes.uml.js: Uncaught TypeError: Cannot read property 'Generic' of undefined

joint.shapes.uml.js: Uncaught TypeError: Cannot read property 'Generic' of undefined

我在 Angular 8 中显示来自 JointJS 演示 https://github.com/clientIO/joint/tree/master/demo/umlcd 的 uml 图,但出现错误:joint.shapes.uml.js:13 Uncaught TypeError: Cannot read 属性 'Generic' 未定义

在 joint.shapes.uml.js:13
在 joint.shapes.uml.js:280

joint.shapes.uml.js: https://github.com/clientIO/joint/blob/master/dist/joint.shapes.uml.js

我做错了什么?

这是我的代码:

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "Inspector": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/Inspector",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "./node_modules/jointjs/dist/joint.css",
              "./src/app/shared/umlcd.css"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.js",
              "./node_modules/lodash/lodash.js",
              "./node_modules/backbone/backbone.js",
              "./plugin/joint.shapes.uml.js",
              "./node_modules/jointjs/dist/joint.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "Inspector:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "Inspector:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "Inspector:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "Inspector:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "Inspector:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "Inspector"
}

带有 jointJS 演示代码的 Typescript 文件:

(我不得不在演示代码中进行更改,例如:每个图实体必须有一个属性和方法数组,并且 属性 name 必须是 String 类型[] 而不是 String。如果没有这些更改,演示代码将无法运行。)

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

import * as _ from 'lodash';
import * as joint from 'jointjs';

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.css']
})
export class DemoComponent implements OnInit{

  constructor() { }

  ngOnInit() {
    this.createDiagram();
  }

  private createDiagram(): void {
    var graph = new joint.dia.Graph();

    new joint.dia.Paper({
        el: document.getElementById('paper'),
        width: 1000,
        height: 600,
        model: graph,
        gridSize: 1,
    });

    var uml = joint.shapes.uml;

    var classes = {

        mammal: new uml.Interface({
            position: { x:300  , y: 150 },
            size: { width: 240, height: 100 },
            name: ['Mammal'],
            attributes: ['dob: Date'],
            methods: ['+ setDateOfBirth(dob: Date): Void','+ getAgeAsDays(): Numeric'],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#feb662',
                    stroke: '#000000',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-rect': {
                    fill: '#fdc886',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#fdc886',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-text': {
                    ref: '.uml-class-attrs-rect',
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                },
                '.uml-class-methods-text': {
                    ref: '.uml-class-methods-rect',
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                }

            }
        }),

        person: new uml.Abstract({
            position: { x:300  , y: 300 },
            size: { width: 260, height: 100 },
            name: ['Person'],
            attributes: ['firstName: String','lastName: String'],
            methods: ['+ setName(first: String, last: String): Void','+ getName(): String'],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#68ddd5',
                    stroke: '#ffffff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-rect': {
                    fill: '#9687fe',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#9687fe',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-text, .uml-class-attrs-text': {
                    fill: '#fff'
                }
            }
        }),

        bloodgroup: new uml.Class({
            position: { x:20  , y: 190 },
            size: { width: 220, height: 100 },
            name: ['BloodGroup'],
            attributes: ['bloodGroup: String'],
            methods: ['+ isCompatible(bG: String): Boolean'],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#ff8450',
                    stroke: '#fff',
                    'stroke-width': 0.5,
                },
                '.uml-class-attrs-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-text': {
                    ref: '.uml-class-attrs-rect',
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                },
                '.uml-class-methods-text': {
                    ref: '.uml-class-methods-rect',
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                }
            }
        }),

        address: new uml.Class({
            position: { x:630  , y: 190 },
            size: { width: 160, height: 100 },
            name: ['Address'],
            attributes: ['houseNumber: Integer','streetName: String','town: String','postcode: String'],
            methods: [],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#ff8450',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-text': {
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                }
            }

        }),

        man: new uml.Class({
            position: { x:200  , y: 500 },
            size: { width: 180, height: 50 },
            name: ['Man'],
            attributes: [],
            methods: [],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#ff8450',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                }
            }
        }),

        woman: new uml.Class({
            position: { x:450  , y: 500 },
            size: { width: 180, height: 50 },
            name: ['Woman'],
            attributes: [],
            methods: ['+ giveABrith(): Person []'],
            attrs: {
                '.uml-class-name-rect': {
                    fill: '#ff8450',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-attrs-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-rect': {
                    fill: '#fe976a',
                    stroke: '#fff',
                    'stroke-width': 0.5
                },
                '.uml-class-methods-text': {
                    'ref-y': 0.5,
                    'y-alignment': 'middle'
                }
            }
        })
    };

    Object.keys(classes).forEach(function(key) {
        graph.addCell(classes[key]);
    });

    var relations = [
        new uml.Generalization({ source: { id: classes.man.id }, target: { id: classes.person.id }}),
        new uml.Generalization({ source: { id: classes.woman.id }, target: { id: classes.person.id }}),
        new uml.Implementation({ source: { id: classes.person.id }, target: { id: classes.mammal.id }}),
        new uml.Aggregation({ source: { id: classes.person.id }, target: { id: classes.address.id }}),
        new uml.Composition({ source: { id: classes.person.id }, target: { id: classes.bloodgroup.id }})
    ];

    Object.keys(relations).forEach(function(key) {
        graph.addCell(relations[key]);
    });
  }

}

在您的 angular.json 中,要包含在构建中的脚本顺序是错误的。

joint.shapes.uml.js应该在joint.js执行后执行。

顺序正确

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