javascript - 关节.shapes.uml.js : Uncaught TypeError: Cannot read property 'Generic' of undefined

标签 javascript angular typescript uml jointjs

我显示来自 JointJS 演示的 uml 图 https://github.com/clientIO/joint/tree/master/demo/umlcd在 Angular 8 中,我收到错误: joint.shapes.uml.js:13 Uncaught TypeError: Cannot read property 'Generic' of undefined

在 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",
            ]

关于javascript - 关节.shapes.uml.js : Uncaught TypeError: Cannot read property 'Generic' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60310331/

相关文章:

javascript - 如何将来自不同组件的 firebase auth 和 Cloud Firestore 作为单个 firebase 应用程序使用

javascript - 根据数组中的值过滤对象

css - 如何在 Angular 中设置特定组件的背景?

node.js - 调试 TypeScript VS Code 时的未绑定(bind)断点

javascript - 为什么我需要使用 `setTimeout()` 来使 `jquery.animate` 在 Backbone View 中工作?

javascript - 如何将对象数组中的某些属性合并到另一个数组中?

Angular Material : recalculate position of mat-menu on height change

javascript - 检查任何对象的 'undefined' 或 'null'

angular - Ionic2:如何在每个 subview 中使用不同的 ionic 菜单

arrays - 以 Angular/ typescript 将类数组中的类对象推送