Angular 7: Uncaught ReferenceError: global is not defined when adding package

前端 未结 3 598
借酒劲吻你
借酒劲吻你 2020-12-17 10:58

I am building an Angular 7 app, and when i add a package npm install dragula --save and import this into the pollyfills.ts file i get this error:

3条回答
  •  青春惊慌失措
    2020-12-17 11:17

    In my vision the best solution is to add this code in your index.html for testing

    
    

    And this code to build for production (install domino ... with npm install first)

    const domino = require('domino');
    const fs = require('fs');
    const path = require('path');
    const template = fs
      .readFileSync(path.join('dist/browser', 'index.html'))
      .toString();
    const window = domino.createWindow(template);
    
    // Ignite UI browser objects abstractions
    (global as any).window = window;
    (global as any).document = window.document;
    (global as any).Event = window.Event;
    (global as any).KeyboardEvent = window.KeyboardEvent;
    (global as any).MouseEvent = window.MouseEvent;
    (global as any).FocusEvent = window.FocusEvent;
    (global as any).PointerEvent = window.PointerEvent;
    (global as any).HTMLElement = window.HTMLElement;
    (global as any).HTMLElement.prototype.getBoundingClientRect = () => {
        return {
          left: '',
          right: '',
          top: '',
          bottom: ''
      };
    };
    
    // If using IgxIconService to register icons
    (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
    
    // Other optional depending on your application configuration
    (global as any).object = window.object;
    (global as any).navigator = window.navigator;
    (global as any).localStorage = window.localStorage;
    (global as any).DOMTokenList = window.DOMTokenList;
    

提交回复
热议问题