简体   繁体   English

在 Angular 中使用插件导入 svg.js

[英]Import svg.js with plugins in Angular

I have difficulties importing svg.js with it's plugins using typescript in an Angular 5 project.我在 Angular 5 项目中使用打字稿导入 svg.js 及其插件时遇到困难。

With npm I installed:使用 npm 我安装了:

In ticket.component.ts: Imported svg.js as Svg type and this way I can draw a rectangle, this code works:在 ticket.component.ts: Imported svg.js as Svg type 中,这样我可以绘制一个矩形,此代码有效:

import * as Svg from 'svg.js'
...
export class TicketComponent implements OnInit {

  ngOnInit() {
    let draw = Svg('ticket-svg').size(1000, 500);
    let rect = draw.rect(800, 300).attr({fill: '#fff', stroke: '#000', 'stroke-width': 1});
  }
...
}

But I cannot use functions from the svg.draggable plugin, because it's not imported:但是我不能使用 svg.draggable 插件中的函数,因为它没有被导入:

rect.draggable()
// TS2339: Property 'draggable' does not exist on type 'Rect'.

I want something like this:我想要这样的东西:

import * as Svg from {'svg.js', 'svg.draggable.js', 'svg.whatever.js'}

In the GitHub example code of svg.draggable.js they just add these two lines in the html and that's all:在 svg.draggable.js 的 GitHub 示例代码中,他们只是在 html 中添加了这两行,仅此而已:

<script src="svg.js"></script>
<script src="svg.draggable.js"></script>

In angular with typescript, this is completely different because I need the import in the ticket.component.ts file, not in the html.在打字稿的角度中,这是完全不同的,因为我需要在 ticket.component.ts 文件中导入,而不是在 html 中。

So how to import svg.js with multiple plugins from node_modules as Svg type?那么如何从 node_modules 导入带有多个插件的 svg.js 作为 Svg 类型?

Update 1:更新 1:

I read a recommendation to import the typings for these 3rd party js libraries, so I installed:我阅读了为这些 3rd 方 js 库导入类型的建议,所以我安装了:

"@types/svg.js": "^2.3.1",
"@types/svgjs.draggable": "0.0.30"

Update 2:更新 2:

Tried to merge the two js file into one file and import the merged version, but for some reason this hacky way does not work either...试图将两个 js 文件合并为一个文件并导入合并后的版本,但由于某种原因,这种 hacky 方式也不起作用......

Thanks in advance!提前致谢!

Unfortunately svgjs.draggable.js doesn't have a definition type file so you can't import it with the ES6 import/export like svg.js.不幸的是svgjs.draggable.js没有定义类型文件,所以你不能像 svg.js 一样使用ES6 导入/导出来导入它。 the definition type file is the file that has the extension d.ts which you can find in the root of the library.定义类型文件是具有扩展名d.ts的文件,您可以在库的根目录中找到该文件。

Also in our case svgjs.draggable extends svg.js.同样在我们的例子中svgjs.draggable扩展了 svg.js。 So we need to import svgjs.draggable after svg.js .所以我们需要在svgjs.draggable之后导入svg.js So how to do that?那么怎么做呢? The solution is to import them both either from index.html or using angular-cli.json .解决方案是从index.html或使用angular-cli.json导入它们。 I'll choose the latter one.我会选择后者。

First install both libraries:首先安装两个库:

$ npm i svg.js -s
$ npm i svg.draggable.js -s

Add to angular-cli.json添加到 angular-cli.json

  "scripts": [
     ...
    "../node_modules/svg.js/dist/svg.min.js",
    "../node_modules/svg.draggable.js/dist/svg.draggable.min.js"
     ...
],

Next tell typescript to stop complaining接下来告诉打字稿停止抱怨

We'll do this by using the following syntax declare const which tells typescript that the import does really exist but you just can't know it because you didn't find the definition types file.我们将通过使用以下语法declare const来做到这一点,它告诉typescript导入确实存在,但您只是不知道它,因为您没有找到定义类型文件。 So in a component I did a small demo.所以在一个组件中我做了一个小演示。

import { Component, OnInit } from '@angular/core';
declare const SVG:any;

    @Component({
      selector: 'app-root',
      template: `<div id="canvas"></div>`,
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
      title = 'app';

      ngOnInit() {
        const draw = SVG('canvas').size(400, 400);
        const rect = draw.rect(100, 100);
        rect.draggable();

      }

Update: Opps...realized your problem was with svg.draggable.js not just svg.js.更新: Opps...意识到你的问题出在 svg.draggable.js 而不仅仅是 svg.js。 I'm going to leave this here if anyone else needs this guidance.如果其他人需要此指导,我将把它留在这里。 Sorry for any confusion.很抱歉有任何混淆。

The following worked for my setup (Angular 9, Typescript, SVG.js v3.0).以下内容适用于我的设置(Angular 9、Typescript、SVG.js v3.0)。

Install SVG.js安装 SVG.js

npm install --save @svgdotjs/svg.js

Import into Component导入组件

Per the SVG.js Getting Started guide , import SVG into your component.根据SVG.js 入门指南,将 SVG 导入到您的组件中。

import { SVG } from '@svgdotjs/svg.js'

Use SVG as wanted根据需要使用 SVG

export class MyComponent implements OnInit {

  constructor() {}

  ngOnInit(): void {

    // don't forget to add "#" CSS selector to the name of the DOM element.
    const draw = SVG().addTo('#canvas').size(400, 400);
    const rect = draw.rect(100, 100, );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM