0

我有一个使用 Typescript 构建的基本 Stencil 应用程序。我没有使用 Angular。我无法使用 svg.js。

这是我的完整组件:

import { Component, State } from '@stencil/core';
import * as moment from 'moment';
import * as SVG from 'svg.js';

@Component({
  tag: 'astro-sandbox',
  styleUrl: 'sandbox.css'
})
export class AstroSandbox {
  componentDidLoad() {
    // console.info(SVG)
    let drawing = SVG('drawing');
  }

  render() {
    return (
      <div>
        <div id="drawing">
        </div>
      </div>
    );
  }
}

这失败了{ Error: Cannot call a namespace ('SVG')...

我尝试查看 SVGconsole.info('SVG')并编译,但浏览器给了我错误 TypeError: Cannot set property 'SVG' of undefined

我知道我的应用程序可以正常工作,并且 moment.js 可以正常工作,因此不存在 TS 配置问题。VS Code 中用于 svg.js 的 Intellisense 也可以正常工作。

4

1 回答 1

1

这是 svg.js 中的一个已知错误。您可以在此处查看相应的问题。问题是,svg.js 将自身添加到this. 在 ECMAscript 262this在此上下文中未定义以防止乱扔窗口对象。同时你可以使用这个 PR 的代码:https ://github.com/svgdotjs/svg.js/pull/867

于 2018-06-14T18:44:49.727 回答