Angular 10 Global is not defined at ../node_modules/socket.io-parser/is-buffer.js

問題描述:
Angular項目中使用了socketio-client庫,

{
    "dependencies": {
    ...
    "@feathersjs/socketio-client": "^4.4.3",
    ...
  },
}

使用:

import socketio from '@feathersjs/socketio-client'

@Injectable()
export class Socket {
  public socket: SocketIOClient.Socket
  public app: any

  constructor() {
    this.app = feathers()
      .configure(socketio(this.socket, {timeout: 20000}))
      .configure(authentication({path: '/login', storage: window.localStorage}))
  }
}

運行後報錯

global is not defined at Object…/node_modules/socket.io-parser/is-buffer.js

說在Object ./node_modules/socket.io-parser/is-buffer.js中沒有定義全局變量

解決辦法:
polyfills.ts file中添加

// polyfills.ts
(window as any).global = window

在這裏插入圖片描述
參考:Global is not defined at …/node_modules/socket.io-parser/is-buffer.js

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章