Webpack,Babel:ES6导入与Fabric.js的需求 [英] webpack, babel: es6 import vs. require for Fabric.js

查看:118
本文介绍了Webpack,Babel:ES6导入与Fabric.js的需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发工具链中使用webpack和babel;运行以下代码时:

I am using webpack and babel in my development tool chain; when running the following code:

import * as fabric from 'fabric';

var canvas = new fabric.Canvas('canvas');

我收到以下错误:

_fabric2.default.Canvas is not a constructor

如果我使用require('fabric');代替,则相同的代码可以正常工作 import.

Whereas the same code works fine if I use require('fabric'); instead of import.

我尝试了不同的呼叫import的方式,但是没有一个起作用.

I tried different ways of calling import but none of them worked.

我的整理工具抱怨未定义的fabric变量,因此我希望对其进行正确定义.令人惊讶的(对我来说),这段代码 也不起作用:

My linting tool complains of the undefined fabric variable, so I would like to have it properly defined. Surprisingly (for me), this code doesn't work neither:

var fabric = require("fabric");

在这种情况下,我收到以下错误消息:

I get the following error in this case:

fabric.Canvas is not a constructor

我在做什么错了?

推荐答案

在当前使用NPM中的fabric设置中,我使用

In my current setup using fabric from NPM, I use

import {fabric} from 'fabric'

import {fabric} from 'fabric'

访问结构全局对象.

这篇关于Webpack,Babel:ES6导入与Fabric.js的需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆