Share data between Angular 6

Asked

Viewed 4,510 times

0

I have a Navbar Component that sits at the root of the project, and I have on the side of that another that is a part. Ex:

navbar
componenet1[
    component2
    component3
    component4[
        component5
        component6
    ]

]

I need to pass a data from Component 6 to navbar, but only when I enter the route of this Component, does anyone have an idea of how to do this to help me.

  • 1

    doesn’t it pay to do a service for it? because if you do Output up to the level of the navbar will get mt ugly

  • The fuck is that I need not redo requisition, then I even have service for it. The problem is the route check in this case;

  • a search in Snapshot of the angular.

  • You can always try using store for your application this will give you access to the data in different parts of the application (eg https://ngrx.io/guide/store)

1 answer

1

It has two main ways of sharing a state between two components that have no parent/child relationship. The first would be with Subject behavior with a service injected into the two components otherwise it would be by a state manager like the ngrx which follows the Redux standard.

Example with Subject behavior:

data.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { Data } from '../entities/data';

@Injectable()
export class DataService {

  private dataSource = new BehaviorSubject<SnapshotSelection>(new Data());
  data = this.dataSource.asObservable();

  constructor() { }

  updatedDataSelection(data: Data){
    this.dataSource.next(data);
  }

}

Its Component

constructor(private dataService: DataService) { }

dataService.data.subscribe(data => {
// use os dados aqui
})

dataService.updateData(newData);// para atualizar os dados

Source: https://medium.com/@weswhite/angular-behaviorsubject-service-60485ef064fc

  • Why did you use the Decorator @Injectable()? As far as I know it is used when injecting a service into another! This line is an injection? private dataSource = new BehaviorSubject<SnapshotSelection>(new Data());

  • The @Injectable() means that the angular will handle the dependency Injection for vc, so you can start this service in any constructor whether it’s a component or another service that the angular injects it for you. In this case you inject the service in the two components you want to communicate.

  • But in this case, I need the data to go to the navbar only when I enter the route la of the Component 6, the problem is there, I do not have an event that checks if the route has changed, this is more disturbing me

  • vc can use the dataService.updateData(newData); in the init of the Component 6 and use an if in the navbar to know if the data came yet or not.

  • By chance this information came from this article: https://medium.com/@weswhite/angular-behaviorsubject-service-60485ef064fc ?

  • I don’t remember now but there are several articles on behaviorSubject

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.