Angular 尝试使用ngxs将对象保护到localStorage
我正试图使用'ngxs/storage plugin'插件版本3.5.1在浏览器的localStorage中保存一个状态对象。使用ngxs的原因是,我们将ngxs用于所有其他状态,并结合http客户端将状态保存在后端 我在网上搜索了一个例子,因为ngxs的文档在这一点上非常差,但是结果也很差 这里是我已经编写的代码。该州运作良好,但在我开始添加本地存储插件后,我就迷失了方向 settings.actions.tsAngular 尝试使用ngxs将对象保护到localStorage,angular,typescript,local-storage,ngxs,Angular,Typescript,Local Storage,Ngxs,我正试图使用'ngxs/storage plugin'插件版本3.5.1在浏览器的localStorage中保存一个状态对象。使用ngxs的原因是,我们将ngxs用于所有其他状态,并结合http客户端将状态保存在后端 我在网上搜索了一个例子,因为ngxs的文档在这一点上非常差,但是结果也很差 这里是我已经编写的代码。该州运作良好,但在我开始添加本地存储插件后,我就迷失了方向 settings.actions.ts export class SetShowHelp { static reado
export class SetShowHelp {
static readonly type = '[MySettings] Set my configuration for the flag showHelp';
constructor(public showHelp: boolean) {}
}
settings.state.ts
import { State, Action, StateContext, Selector } from '@ngxs/store';
import {
SetShowHelp
} from '../actions/mysettings.actions';
export class SettingsStateModel {
public showHelp: boolean;
public hideToolbar: boolean
}
@State<SettingsStateModel>({
name: 'my-settings',
defaults: {
showHelp: true,
hideToolbar: false
}
})
export class SettingsState {
@Selector()
static getShowHelp(state: SettingsStateModel) {
return state.showHelp;
}
constructor() { }
@Action(SetShowHelp)
setShowHelp({ patchState }: StateContext<SettingsStateModel>, {showHelp}: SetShowHelp) {
// patch the state and write into local storage
patchState({
showHelp: showHilfe
});
}
.....
}
如何将“SettingsStateModel”对象写入本地存储 问题是我所在州的名称中包含一个负号,这是不允许的。我把名字改成了“mysettings”。我还将
app.module.ts
更改为:
NgxsModule.forRoot([SettingsState]),
NgxsStoragePluginModule.forRoot({
key: 'mysettings'
}),
经过所有的改变,一切都很好
NgxsModule.forRoot([SettingsState]),
NgxsStoragePluginModule.forRoot({
key: 'mysettings'
}),