Next, let's talk about plugins. Similar to Redux's meta reducers, we have a plugin interface that allows you to build a global plugin for your state.
All you have to do is call withNgxsPlugin with a plugin class. If your plugins have options associated with them, we also suggest defining an injection token.
You can also use pure functions for plugins. The above example in a pure function would look like this:
import { NgxsNextPluginFn } from'@ngxs/store/plugins';exportfunctionlogPlugin(state:any, action:any, next:NgxsNextPluginFn) {// Note that plugin functions are called within an injection context,// allowing you to inject dependencies.constoptions=inject(NGXS_LOGGER_PLUGIN_OPTIONS);console.log('Action started!', state);returnnext(state, action).pipe(tap(result) => {console.log('Action happened!', result); });}
To register a plugin with NGXS, add the plugin to your provideStore as an NGXS feature and optionally pass in the plugin options like this: