【问题标题】:Module not found: Error: Can't resolve 'fs' in 'Component ' ? file.json Angular 6未找到模块:错误:无法解析“组件”中的“fs”?文件.json Angular 6
【发布时间】:2019-02-13 17:15:01
【问题描述】:

import fs

fs = requier('fs')

fs index.d.ts I can see fs library but I can't use

Error in cmd

如果我写 require cmd 错误,我的问题 另外,在 npm install fs 的安装 fs 之后。

安装后或安装 fs 前出现同样的错误

【问题讨论】:

  • fs 是 NodeJS 的一个模块。 NodeJS 是服务器端的。 Angular 是一个用于创建前端应用程序的框架。看看这个:github.com/angular/angular-cli/issues/5324
  • 好的,我的问题怎么解决,我要写json文件
  • 我相信这个问题会有所帮助:stackoverflow.com/questions/30288087/… 使用客户端代码将静态文件写入文件系统是不可能的。您必须选择一种替代方法。
  • 谢谢,但是可以告诉我如何直接保存数据,例如saveAs(ex.json);
  • 我在过去 3 小时内看到了你的链接,但是给我链接我想直接指向 json 文件,我很困惑。

标签: json typescript angular6 fs


【解决方案1】:

我使用文件保护程序连接 Node.js 和 Angular 6

npm install file-saver --save
import { saveAs } from 'file-saver';


const jsonObject: object = {
      'City': [
        {
          'id': 1,
          'name': 'Basel',
          'founded': -200,
          'beautiful': true,
          'data': 123,
          'keywords': ['Rhine', 'River']
        },
        {
          'id': 1,
          'name': 'Zurich',
          'founded': 0,
          'beautiful': false,
          'data': 'no',
          'keywords': ['Limmat', 'Lake']
        }
      ]
    };


const blob = new Blob([JSON.stringify(jsonObject)], {type : 'application/json'});
saveAs(blob, 'abc.json');

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 2018-06-29
    相关资源
    最近更新 更多