Search code examples
angularngxs

Custom deserializer for a particular state NGXS Storage Plugin


The storage plugin specify that I can use custom deserializer. The problem is it doesnt provide anything on how to use it. Is the deserializer provided be used on all state classes?

What I need is to have a specific deserializer in each States.

Thanks!


Solution

  • Deserialize is one of the options of NgxsStoragePluginModule:

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

    You set one for all. I personally didn't try this. But according to docs, this way we can define options to storage plugin.