我想在我的 ts 组件中导入 fs 模块
import * as fs from 'fs';
我需要它们来使用方法 writeFile
,因为我想从 HTML 表单中获取数据并将其写入 JSON 文件。
但是控制台向我抛出了那个错误:
error TS2307: Cannot find module 'fs' or its corresponding type declarations.
new-card.component.ts
import { Component, OnInit } from '@angular/core';
import jCardList from '../mock-cards.json';
import { Card } from '../ICard';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import * as fs from 'fs';
@Component({
selector: 'app-new-card',
templateUrl: './new-card.component.html',
styleUrls: ['./new-card.component.css']
})
export class NewCardComponent implements OnInit {
fg: FormGroup;
constructor(private fb:FormBuilder) {
this.fg = this.fb.group({
id: [{value: 1, disabled: true},[Validators.required]],
nome: [{value: null, disabled: false},[Validators.required]],
manaCost:[{value: null, disabled: false},[Validators.required]],
colorIdentity:[{value: null, disabled: false},[Validators.required]],
type:[{value: null, disabled: false},[Validators.required]],
subType:[{value: null, disabled: false}],
effect:[{value: null, disabled: false}],
att:[{value: null, disabled: false}],
def:[{value: null, disabled: false}],
price:[{value: null, disabled: false}],
});
}
new-card.component.html
<div>
<form [formGroup]="fg" (ngSubmit)="onClick(fg)" >
<table>
<tr>
<td>Id !:</td>
<td><input type="number" id="Id" formControlName="id"></td>
</tr>
<tr>
<td>Nome !:</td>
<td><input formControlName="nome"></td>
</tr>
<tr>
<td>Costo in mana !:</td>
<td><input type="number" id="manaCost" formControlName="manaCost"></td>
</tr>
<tr>
<td>Colore !:</td>
<td><input id="colorIdentity" formControlName="colorIdentity"></td>
</tr>
<tr>
<td>Tipo !:</td>
<td><input id="tipe" formControlName="type"></td>
</tr>
<tr>
<td>Sottotipo:</td>
<td><input id="subTipe" formControlName="subType"></td>
</tr>
<tr>
<td>Effetto:</td>
<td><input id="effect" formControlName="effect"></td>
</tr>
<tr>
<td>Forza:</td>
<td><input type="number" id="att" formControlName="att"></td>
</tr>
<tr>
<td>Costituzione:</td>
<td><input type="number" id="def" formControlName="def"></td>
</tr>
<tr>
<td>Prezzo:</td>
<td><input type="number" id="price" formControlName="price"></td>
</tr>
</table>
<button type="submit" [disabled]="fg.invalid" >Conferma</button>
</form>
</div>
我已经尝试使用 npm install @types/node --save-dev
并将其添加到 compilerOptions tsconfig.json
内的文件 "types": ["node"],"typeRoots": ["node_modules/@types"],
中
因此,如果有人可以帮助我解决此错误或为我提供不同的解决方案,我会很高兴。
答案 0 :(得分:3)
fs
它是 Node.js 中的一个内置包,它是服务器端。您的代码是客户端,您无法使用它。