【问题标题】:creating my own shape definition in jointjs doesn't work in angular7在jointjs中创建我自己的形状定义在angular7中不起作用
【发布时间】:2019-10-19 00:20:40
【问题描述】:

我正在使用 angular 7 和 joint.js 来创建我自己的形状定义。 例如,

joint.shapes.devs.Model.define("devs.Type",
  {
    size: {
      width: 300,
      height: "auto"
    }
 });

joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
    attrs: {
        body: {
            rx: 10, // add a corner radius
            ry: 10,
            strokeWidth: 1,
            fill: 'cornflowerblue'
        },
        label: {
            textAnchor: 'left', // align text to left
            refX: 10, // offset text from right edge of model bbox
            fill: 'white',
            fontSize: 18
        }
    }
});






var rect2 = new joint.shapes.examples.CustomRectangle();
var a1 = new joint.shapes.devs.Type(node);

编译代码给了我两个错误

错误 TS2339:“typeof devs”类型上不存在属性“Type” 错误 TS2341:类型“typeof”上不存在属性“示例” 形状”。

我该如何解决这个问题?


另外,客户Link定义了transitionColor方法,但是在paper.on("link:mouseover",....中不能调用,报错

“链接”类型上不存在属性“transitionColor”。

joint.dia.Link.define(
      "devs.Link",
      {
        attrs: {
          line: {
            connection: true
          },
          wrapper: {
            connection: true,
            strokeWidth: 2,
            strokeLinejoin: "round"
          }
        }
      },
      {
        markup: [
          {
            tagName: "path",
            selector: "wrapper",
            attributes: {
              fill: "none",
              cursor: "pointer",
              stroke: "transparent"
            }
          },
          {
            tagName: "path",
            selector: "line",
            attributes: {
              fill: "none",
              "pointer-events": "none"
            }
          }
        ],
        transitionAsync: function (...args) {
          return new Promise(resolve => {
            this.transition(...args);
            this.on("transition:end", () => {
              resolve();
            });
          });
        },
        transitionColor: function (color, { delay = 0, duration = 100 }) {
          return this.prop("attrs/line/stroke", color);
        },
        transitionOpacity: function (opacity, { delay = 0, duration = 100 }) {
          return this.prop("attrs/line/opacity", opacity);
        }
      }
    );


paper.on("link:mouseover", (linkView: any) => {
      const links = this.graph.getLinks();
      links.map(link => {
        if (link === linkView.model) {
          return;
        }
        link.transitionColor(theme.colors.line.inactive, {duration:500});
        link.toBack()
      });
});

【问题讨论】:

    标签: angular typescript jointjs


    【解决方案1】:

    您的问题在于 TypeScript,而不是 Angular,并且可能与库可用的类型有关。

    您的错误消息显示为'Type' does not exist on type 'typeof devs'。这意味着您的变量 devs 没有类型,因此 TypeScript 从变量定义中动态推断类型:

    // devs is not declared this way, but this is just to make the point
    const devs = {
      prop1: string;
      prop2: number;
    };
    
    // you can add extra properties with the JavaScript square brackets access:
    devs['Type'] = function() { ... }
    
    devs['Type']() // <- this call works
    
    devs.Type() // <-- compile time exception: Type is not present in the type.
    

    要摆脱这个难题,您可以:

    1. 了解库的可用类型是否存在问题,并加载适当的类型文件(来自某个库的 .d.ts 文件)。如果其他人打扰定义类型签名,这是迄今为止的首选选项,因为您的代码是在编译时检查的。但是,如果您无权访问类型文件,则最终编译它们是不切实际的。
    2. 退出 TypeScript 类型系统,只使用 any。如果你知道这个库,并且你习惯于在普通的 JavaScript 中使用它,你可以声明你正在使用的对象不是类型的:
    // this out of any class body declares that somewhere in window, a joint object exists, and we don't know anything about it
    declare const joint: any;
    
    // or, when calling a subsection of joint with bad or incomplete typings:
    const a1 = new (joint.shapes.devs as any).Type(node);
    // same meaning, different syntax:
    const a2 = new (<any>joint.shapes.devs).Type(node);
    
    // you can just get an untyped reference to devs and use it:
    const untypedDevs: any = joint.shapes.devs;
    const a3 = new untypedDevs.Type(node);
    

    一般来说,您应该尝试至少学习一点 TypeScript,因为那是您现在正在使用的语言。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2016-09-06
      • 2021-12-26
      • 2018-12-18
      相关资源
      最近更新 更多