Pass data from one component to another angular 4

0

I am using angular 4 and the problem is as follows, I do not know how to pass a variable from one component to another, in component 1 I get a data and I would like to save it in a kind of global variable, to be able to use it in the other components PS: I can not use LocalStorage or SesionStorage because they are very sensitive data of the client.

    
asked by felipe caro 09.02.2018 в 15:04
source

2 answers

1

If what you want to send (variable) is from parent to child you have to use the decorator @Input() , if it is the other way around you have to use @Output and EventEmitter , if it is between components 'siblings' the best option is by means of a service and rxjs . Share some code to see what you have so far

    
answered by 09.02.2018 в 18:04
0

You can create this component for global variables: - "app.global.ts"

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

@Injectable()
export class AppGlobals {
    readonly baseAppUrl: string = 'http://localhost:57431/';
    readonly baseAPIUrl: string = 'https://api.github.com/';
}

then you use it in your project calling it like this:

import { AppGlobals } from '../shared/app.globals';

lo llamas en el constructor:
    constructor(private userService: UserService, private _global: AppGlobals) { }

//GET USERS SERVICE ON PAGE LOAD.

 this.userService.getAPIUsers(this._global.baseAPIUrl + users/hadley/orgs').subscribe(data => this.users = data);

================================================================================================= ==== for global variables.

    
answered by 11.08.2018 в 15:46