Vijay Vijay - 17 days ago 81
TypeScript Question

How to use session variable from one TS file to another TS

I have session variable in

home.component.ts
named
session
. I want to use this variable in
user.compnent.ts
and also in
dashboard.component.ts
. Below is my session variable in
home.component.ts
:

var session = sessionStorage.getItem('session');


and below is my code in
user.component.ts
:

import { HomeComponent } from '../Components/home.component';
constructor(private fb: FormBuilder, private _userService: UserService,
private _HomeComponent: HomeComponent, private router: Router) { }


But I couldn't get the session. I'm new to
Angular
.

Answer Source

Since you are alreayd using sessionStorage, the variables stored in will be accessible throught the page. so you need the following in the dashboard.component.ts

var session = sessionStorage.getItem('session');