特定状态 NGXS 存储插件的自定义解串器

Custom deserializer for a particular state NGXS Storage Plugin

存储插件指定我可以使用自定义解串器。问题是它没有提供任何关于如何使用它的信息。提供的解串器是否用于所有状态 类?

我需要的是在每个州都有一个特定的解串器。

谢谢!

反序列化NgxsStoragePluginModule的选项之一:

@NgModule({
  imports: [
    NgxsModule.forRoot([]),
    NgxsStoragePluginModule.forRoot({
      deserialize(data) {
        return JSON.parse(data) // by default it returns parsed data
      }    
    })
  ],
})

你为所有人设置一个。我个人没有尝试过这个。但是根据文档,通过这种方式我们可以定义存储插件的选项。

正确的方法是先定义你的导出函数,否则AOT编译会失败。

@NgModule({
  declarations: [AppComponent],
  imports: [
    NgxsStoragePluginModule.forRoot({
      deserialize(data) {
        // Do deserialization
      },
    }),
...

这有效with/without AOT:

export function deserialize(data: any) {
  // Do deserialization
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    NgxsStoragePluginModule.forRoot({
      deserialize,
    }),
...

https://github.com/ngxs/store/issues/1123#issuecomment-605933173