【问题标题】:Angular 10: Buffer is not definedAngular 10:未定义缓冲区
【发布时间】:2020-08-27 18:34:26
【问题描述】:

我需要一些帮助,因为我不知道该怎么做。我必须在我的 Angular 项目中使用“amqplib”。首先,我尝试使用“amqp-ts”,但一打开浏览器,我就遇到了错误:

经过几次尝试,我已切换到“amqplib”。但是,当我尝试调用库的函数时出现错误:

错误发生在一行:

if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
  module.exports = buffer
} else {
  // Copy properties from require('buffer')
  copyProps(buffer, exports)
  exports.Buffer = SafeBuffer
}

同样,Buffer 没有定义。

我尝试了这里提到的几种解决方案:Angular 6 Uncaught ReferenceError: Buffer is not defined,我最终得到了: - 安装“缓冲”包

-在 polyfills.ts 中

declare var require: any;
declare var global: any;

(window as any).global = window;
// @ts-ignore
window.Buffer = window.Buffer || require('buffer').Buffer;
(window as any).process = {
  version: ''
};

-in index.html

<script>
    var global = global || window;
</script>

尽管如此,他们都没有帮助,我仍然有这些错误:/

【问题讨论】:

    标签: angular amqp node-amqp


    【解决方案1】:

    我正在尝试使用加密浏览器

    我也最终使用 npm 安装了 Buffer 包

    然后我收到一个名为的错误:- 缓冲区不是构造函数 这就是我所做的 -

    在 polyfills.ts 中 -

    import * as Buffer from '../node_modules/buffer/index';
    (window as any).global = window;
    (window as any).process = {};
    (window as any).process = window;
    (window as any).process.browser = true;
    (window as any).process.version = '';
    (window as any).process.versions = { node: false };
    (window as any).global.Buffer = Buffer.Buffer;
    

    【讨论】:

      【解决方案2】:

      我不能代表以前版本的 angular,但代表 angular 11。合并非浏览器 nodejs 函数(如缓冲区)的一种优雅方法是使用 webpack 的 polyfilling 系统

      在根目录中创建一个 webpack.config.js 文件并添加以下行:

      module.exports = {
        node: {
          Buffer: true
        }
      };
      

      然后在 angular.json 文件中,在 buildtestserve json 下添加 extraWebpackConfig 选项设置

      "architect": {
              "build": {
                "builder": "ngx-build-plus:browser",
                "options": {
                  "extraWebpackConfig": "webpack.config.js",
      

      更多详情:Buffer is not defined

      【讨论】:

      • 不适合我,但我有一个不同的构建器@angular-devkit/build-angular:browser。这是在 ionic4-angular11 应用程序中,顺便说一句。
      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2022-01-21
      • 2022-01-28
      • 1970-01-01
      • 2021-10-12
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多