Global Data in angular2
<p>I am working on angular 2 In which I have <strong>app</strong> component which load other component <strong>router-outlet</strong> and also have the link to login component. but I want the way to hold some <strong>global variable</strong> which will accessible on my <strong>app</strong> component and <strong>login</strong> component so that i can <strong>hide and show the login link</strong>.</p>

<p>Here is my app component:</p>

<pre><code>import {Component, View, Inject} from 'angular2/core';
import {NgIf} from 'angular2/common';
import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS} from 'angular2/router';


import {HomeComponent} from '../home/home';
import {LoginComponent} from '../login/login';

@Component({
    selector: 'app',
})
@View({
    templateUrl: '/scripts/src/components/app/app.html',
    directives: [RouterLink, RouterOutlet, NgIf]
})
export class App {
    constructor(
        @Inject(Router) router: Router
    ) {
        this.devIsLogin=false;
        router.config([
            { path: '', component: HomeComponent, as: 'Home' },
            { path: '/login', component: LoginComponent, as: 'Login' }
        ]);
    }
}
</code></pre>

<p>here is my logincomponent</p>

<pre><code>///&lt;reference path="../../../node_modules/angular2/typings/node/node.d.ts" /&gt;

import {Component, View, Inject} from 'angular2/core';
import {FormBuilder, FORM_DIRECTIVES } from 'angular2/common';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {LoginService} from '../../services/loginService';
import {Router} from 'angular2/router';

@Component({
    selector: 'login',
    providers: [HTTP_PROVIDERS]
})
@View({
    templateUrl: '/scripts/src/components/login/login.html',
    directives: [FORM_DIRECTIVES]
})

export class LoginComponent {
    userName: string;
    password: string;
    showError: boolean;
    constructor(
        @Inject(LoginService) private loginService: LoginService,
        @Inject(Router) private router: Router
    ) {
        this.userName = '';
        this.password = '';
        this.showError = false;
    }
    login() {
        var data = {
            userName: this.userName,
            password: this.password
        }
        this.loginService.login(data, (res) =&gt; {
            this.showError = false;
            // and then we redirect the user to the home
            this.router.parent.navigate(['/Home']);
        }, (err) =&gt; {
            this.showError = true;
        });
    }
}
</code></pre>

<p>after login i have to set some variable which i can access on app component to hide and show login link and also on other component wherever is needed.</p>