Giter VIP home page Giter VIP logo

lb1997ccb / ng-ngrx-storage-sync Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 444 KB

I created a project that aims to streamline state management in Angular applications using NgRx. It also leverages NgRx's powerful features for managing complex application states and integrates NgRx Local Storage for persistent state management.

License: MIT License

HTML 21.54% TypeScript 71.07% SCSS 5.21% Sass 2.19%
angular ngrx-store ngrx-store-devtools ngrx-storage angular18

ng-ngrx-storage-sync's Introduction

Angular NgRx Storage Sync

![Angular NgRx Storage Sync]

Overview

The Angular NgRx Storage Sync project combines Angular with NgRx for advanced state management and synchronized data storage using localStorage. This project ensures seamless synchronization of application state across sessions, providing enhanced persistence and reliability for Angular applications.

Features

  • NgRx Integration: Utilizes NgRx for managing complex application states with predictable state management.

  • LocalStorage Synchronization: Implements synchronized storage mechanisms like localStorage to persist application state between browser sessions.

  • Example: Persistence Service

    The StatePersistenceService ensures that the application state is loaded from localStorage on initialization:

    @Injectable({
      providedIn: "root",
    })
    export class StatePersistenceService {
      constructor(private store: Store<PlanetState>) {
        this.loadState();
      }
    
      loadState() {
        try {
          const storedState = localStorage.getItem("planets");
          if (storedState) {
            this.store.dispatch(loadPlanetsSuccess({ planets: JSON.parse(storedState).planets }));
          }
        } catch (error) {
          console.error("Error loading persisted state:", error);
        }
      }
    }
  • Example: Planets with Details

    The PlanetDetailComponent demonstrates how to display detailed information about planets fetched from an API using NgRx:

    @Component({
      selector: 'app-planet-detail',
      templateUrl: './planet-detail.component.html',
      styleUrls: ['./planet-detail.component.scss']
    })
    export class PlanetDetailComponent implements OnInit {
      planet$: Observable<Planet | null>;
    
      loading$: Observable<boolean>;
      error$: Observable<any>;
    
      constructor(
        private route: ActivatedRoute,
        private store: Store<PlanetState>
      ) {
        this.planet$ = this.store.pipe(
          select(planetSelectors.selectSelectedPlanet),
        );
        this.loading$ = this.store.pipe(
          select(planetSelectors.selectPlanetLoading),
        );
        this.error$ = this.store.pipe(select(planetSelectors.selectPlanetError));
      }
    
      ngOnInit(): void {
        this.route.paramMap.subscribe((params) => {
          const planetId = params.get('id');
          if (planetId) {
            this.store.dispatch(planetActions.loadPlanet({ id: planetId }));
          }
        });
      }
    }

Getting Started

To get started with Angular NgRx Storage Sync, follow these steps:

  1. Clone this repository.
  2. Install dependencies using npm install.
  3. Run the application using ng serve.

Technologies Used

  • Angular
  • NgRx
  • TypeScript
  • HTML/CSS

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Fork the repository and submit a pull request.

ng-ngrx-storage-sync's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.