简体   繁体   English

如何将文件保存在 ngrx 商店中?

[英]How to save files in ngrx store?

Since last week I have migrated my app to ngrx.自上周以来,我已将我的应用程序迁移到 ngrx。 Now I'm facing a problem: My app needs access to File objects the whole time my app runs.现在我面临一个问题:我的应用程序在我的应用程序运行的整个过程中都需要访问 File 对象。 Because ngrx stores JSON objects only, it can't store File objects and other type of classes.因为ngrx只存储JSON对象,它不能存储File对象和其他类型的类。

What is the best way to handle File objects using ngrx?使用 ngrx 处理文件对象的最佳方法是什么? Do I have to convert them to JSON objects and save them to the store or do I have to save them out of the store?我必须将它们转换为 JSON 对象并将它们保存到商店还是必须将它们保存在商店外? Or is there any other way?或者还有其他方法吗?

Context: the app does audio processing and makes use of other text files.上下文:应用程序进行音频处理并使用其他文本文件。

For further processing I need the contents of the file, its name and size.为了进一步处理,我需要文件的内容、名称和大小。 I thought about making a JSON object, but this could be inefficient (because ngrx creates copies of its state):我想过制作一个 JSON object,但这可能效率低下(因为 ngrx 创建了其状态的副本):

{
  name: string;
  size: number;
  buffer: ArrayBuffer;
}

ngrx can store File objects just like any other service. ngrx 可以像任何其他服务一样存储 File 对象。 Because the redux plugin for the browser can't output the File object correctly I thought File objects are not supported by ngrx.因为浏览器的 redux 插件不能 output 文件 object 正确我认为 Ngrx 不支持文件对象。 If I output the current state to the console the list of files is available as it should be.如果我 output 当前 state 到控制台,文件列表应该是可用的。

Saving files, async calls, localStorage/ sessionStorage etc are handled as side effects.保存文件、异步调用、localStorage/ sessionStorage 等作为副作用处理。

Look into Ngrx Effect documentation.查看Ngrx 效果文档。

You should translate files or other complex objects into JSON and store in the store.您应该将文件或其他复杂对象转换为 JSON 并存储在存储中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM