angular - 错误参考错误:“ng build”时“未定义缓冲区”
问题描述
我正在尝试在我的角度组件 ts 中使用 Buffer 来编码授权字符串。
它不是用ng build
. 我尝试npm i @types/node
将“节点”添加到 tsconfig.json 中的类型字段。但是错误仍然存在。
以下是错误详情:
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了,希望不会有问题
推荐阅读
- javascript - 自定义反应钩子不是使用 useState 的函数吗?
- python - NotFittedError:此 LinearRegression 实例尚未拟合。在使用此方法之前使用适当的参数调用“fit”
- linux - 在以“l0”结尾且名称中不包含“*18*”的目录中查找目录
- javascript - Find() 只显示对象中的第一个数组
- sharepoint-online - 如何从在线共享点的站点页面上传和下载 aspx 文件
- javascript - 防止 JSON 对象在 ReactJS 中按字母顺序排序
- c# - 调用从 Azure 管道发送 C# exe 的邮件时出现 SMTP 错误
- react-native - 模拟所需 json 文件的内容
- python - 使用 docker 在 heroku 中部署 Django api rest
- sql - 合并语句插入数据与选择语句 Hana DB