Transfer an Object from the LoginComponent to the ProfilComponent

Beginning with Angular, I am curious about the process of passing an object (connectedUser) from the LoginComponent to the ProfileComponent.

Your help is greatly appreciated. Thank you in advance.

Answer №1

Here is a different approach to try:

create Class RegistrationComponent {
userData = {}

}

within the HTML template :

<app-registration-component [info]="userData"></app-registration-component> 

inside the registration Component :

create class RegistrationComponent {
@input() info : any; // Data is received here 

}

Answer №2

If you're looking for a way to implement this functionality, here's one approach you can take. Start by creating a service called AppStateService (in app-state.service.ts) that will hold and share an object. Feel free to use the code snippet below as a reference:

Here is the content of app-state.service.ts file:

@Injectable({
  providedIn: 'root',
})
export class AppStateService {
  private _connectedUser: any = undefined;

  public get connectedUser(): any {
    return this._connectedUser;
  }
  public set connectedUser(value: any) {
    this._connectedUser = value;
  }
}

Within your login.component.ts file, you can access the AppStateService like this:

constructor(private appState: AppStateService) { }

public onLoggedin(): void {
  this.authService.login(username, password).subscribe(res => {
    this.appState.connectedUser = res.data;
  });
}

In your profile.component.ts file, retrieve the connected user information using the following code:

public connectedUser;

constructor(private appState: AppStateService) { }

ngOnInit(): void {
  this.connectedUser = this.appState.connectedUser;
  console.log('I:--START--:--ProfileComponent OnLoad--:connectedUser/', connectedUser);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to extract a date from Mat-Datepicker without using moment library

I'm encountering an issue with Mat-datepicker in my Angular app. When I manually input the date into the field, it doesn't parse in the correct locale, but it displays correctly when selected from the month view. Recently, I made the switch from ...

Exploring Angular 6 with Universal Karma for effective module testing

Issue I have been facing challenges while testing my Angular 6 application with Karma. I am encountering errors such as: Can't bind to 'ngModel' since it isn't a known property of 'mat-select'. Although the import works in ...

Enhancing Angular2 performance when managing a large number of input elements with two-way binding

My current project involves a component that resembles a spreadsheet, with numerous elements using two-way binding [(ngModel)] to a mutable object. However, when the number of inputs exceeds 100, the UI starts to slow down. After profiling the application ...

POST requests in Angular Universal are making use of the IP address assigned to my server

My Angular Universal application (version 5.2.11) is currently hosted on Heroku, running on a Node server using express. I have implemented rate-limiters in all my POST routes to restrict requests by IP address, checking the request's IP through req.h ...

Selecting only the month and year with the v-date-picker

I am looking for an alternative approach using the v-date-picker component. My requirement is to allow users to only select the year and month, and then close the date picker menu automatically. Below is an example of my HTML code, but I am open to using ...

Steps for adjusting the status of an interface key to required or optional in a dynamic manner

Imagine a scenario where there is a predefined type: interface Test { foo: number; bar?: { name: string; }; } const obj: Test; // The property 'bar' in the object 'obj' is currently optional Now consider a situatio ...

A guide on retrieving the index of an element within a nested array

Is it possible to determine the index of an item within a nested array without knowing how deeply nested it is? For example, given the array arr=[a,[b,[c],[d],e],f,[g],h]. ...

Bring in TypeScript property from an external scope into the current scope

I am encountering an issue with my TypeScript code. Inside the anonymous functions, I am unable to change the properties of the class because they are out of scope. Is there a way to pass them in so that they can be modified? class PositionCtrl { ...

Detecting the check status of a checkbox in react native: a complete guide

I'm currently working on a scenario where I need to implement logic for checking or unchecking a checkbox in React Native. If the checkbox is checked, I want to print one string, and if it's unchecked, I want to print something else. How can I ac ...

Utilizing SVG Images on Buttons within Material Design (MD-Icon)

I have experimented with different methods, but none seem to be effective: <button md-icon-button color="primary"> <md-icon md-svg-src="./assets/img/sprites.svg">menu</md-icon> </button> and also tried this: <md-icon svgSrc= ...

Using TypeScript to define parameter types to update an object's key and value

I need assistance in creating a function that can update an object's value based on the provided key and new value while ensuring type safety. type GroceryStore = { isOpen: boolean; offers: string[]; name: string; }; const myGroceryStore: ...

Tips for separating provider and input components with React Hook Form

Currently, I am working on a project with Next 13.5 using react-hook-form and shadcn-ui. The issue that I have encountered is that creating a form involves too much redundant code. To address this, I abstracted the FormProvider and Input component. The pr ...

Tips for utilizing the onClick handler when mapping through items in React

Currently, I am a student learning about react. During the course of working on a project, I encountered a problem that left me with a question. {pages.map((page, idx) => ( <li key={page.page_id} id={`${idx + 1}`} css={CSSCarouselItem}> < ...

Executing the ngIf directive in Angular 2 when a function or click event occurs

Is there a way to display an element only when a specific function is executed or a particular click event occurs? Here's the html code snippet I'm currently working with: <sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [map ...

Guide to making an array of objects reactively with formControlName

I am currently working with an Angular application. Within this application, I have a reactive form that is declared as form!: FormGroup<GlobalBean> Within the GlobalBean interface, there is an array of another bean defined as: export interface Glo ...

The URL "http://localhost:8100" has been restricted by the CORS policy, as it lacks the necessary 'Access-Control-Allow-Origin' header on the requested resource

`The CORS policy has blocked access to the XMLHttpRequest at 'http://localhost/phpfile/leave-option.php' from the origin 'http://localhost:8100'. This is due to the absence of the 'Access-Control-Allow-Origin' header on the re ...

Encountering an "Offset exceeds DataView bounds" error while trying to load an .STL model in Angular 9

I've been trying to incorporate a 3D model view into my website using the stl-model-viewer provided at: https://www.npmjs.com/package/angular-stl-model-viewer Despite following the installation steps, it seems that I'm unable to get it to work. T ...

I seem to be stuck in an endless cycle with React hooks and I can't figure out the root cause

Explore the example here: https://codesandbox.io/s/wandering-wildflower-764w4 Essentially, my goal is to achieve the following: In the provided example, I have a server validation function that has been mocked. My objective is to maintain the state local ...

Required attributes not found for data type in TypeScript

When the following code snippet is executed: @Mutation remove_bought_products(productsToBeRemoved: Array<I.Product>) { const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine); const reducedPro ...

Properly segmenting sections into components in Angular

My project has a specific folder structure as shown below: https://i.sstatic.net/7oihC.png In my list-page, I perform operations such as create, update, and delete using modal dialogs. I am considering creating 4 pages for each of these CRUD operations a ...