错误参考错误:“ng build”时“缓冲区未定义”

2023-12-24

我正在尝试在我的角度组件 ts 中使用 Buffer 来编码授权字符串。

它没有编译ng build。我试过npm i @types/node并将“node”添加到 tsconfig.json 中的 types 字段。但错误仍然存​​在。

以下是错误详细信息:

ERROR in src/app/register/register.component.ts(40,29): error TS2591: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.

这是register.component.ts的代码

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { User, PricingPlan } from 'api';
import * as EmailValidator from 'email-validator';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
  model = new User();
  passwordRepeat = '';
  pricingPlans: PricingPlan[];
  userExist = false;

  serverUrl = 'http://127.0.0.1:3000/';
  registerSuccess = false;
  registerFailed = false;

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http
      .get(`${this.serverUrl}pricingplans`)
      .subscribe((pricingplans: PricingPlan[]) => {
        this.pricingPlans = pricingplans;
      });
  }

  validateEmail(em) {
    return EmailValidator.validate(em);
  }

  validatePasswords() {
    return this.passwordRepeat === this.model.password;
  }

  save() {
    const auth = 'Basic ' + Buffer.from(this.model.email).toString('base64');

    this.http.get(`${this.serverUrl}user`, {
      headers: {
        Authorization: auth
      }
    }).subscribe((responce: User) => {
      this.userExist = (responce.email === this.model.email);
    });

    if (this.userExist) {
      return;
    }
  }
}

以下是我的 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "node"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

在你的机器上通过此命令安装 Buffer

npm install Buffer

然后你可以在你的 register.component.ts 中导入 Buffer 作为

import * as Buffer from "Buffer";

现在你可以使用Buffer了,希望不会有问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误参考错误:“ng build”时“缓冲区未定义” 的相关文章

随机推荐