Fabric.js with Typescript and Webpack: Canvas is not a constructor

試著忘記壹切 提交于 2019-12-05 09:07:28

The solution lied in the configuration of webpack.mix.js. The exports-loader has to be added for fabric to solve the problem. The follwoing rule were added:

            rules: [
            ...
            {
                test: /fabric(\.min)?\.js$/,
                use: 'exports-loader?fabric',
            },
        ]

and fabric can be used like in variant a:

        import fabric from "fabric";
        const canvas = new fabric.Canvas('my-canvas');

Make sure the exports-loader is installed. I found the hint here: https://github.com/OfficeDev/office-ui-fabric-js/issues/258

This worked for me:

import 'fabric' ;
declare let fabric: any;

var c = new fabric.Canvas('myCanvas') ;

I'm using @types/fabric 1.5.26 on TypeScript 2.5.3 and webpack 3.5.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!