I am currently working on an Angular component that includes a function. Within this function, I need to pass an Object as a parameter and invoke the function with these parameters. It has been some time since I last worked with Angular, where "any" was typically used as the return type for functions. Nowadays, it appears that "undefined" is more common. However, I am unsure of how to correctly write the function call with the appropriate parameters. Any assistance or tips would be greatly appreciated. Thank you in advance!
Below is the code snippet for the component:
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
public interactionID: number = 0;
@Input()
public userdata!: Object;
constructor() {
// Constructor called before ngOnInit()
}
public ngOnInit(): void {
this.NewWorkItem(this.interactionID, this.userdata);
}
public NewWorkItem(interactionID: unknown, userData: { FirstName: unknown; LastName: unknown }): void {
console.log('NewWorkItem call', interactionID, userData);
const event: Event = new CustomEvent ('wde.newWorkItem', {
detail: {
FirstName: userData.FirstName,
LastName: userData.LastName,
InteractionID: interactionID
},
bubbles: true,
cancelable: true,
composed: false,
});
console.log('NewWorkItem event', event);
window.dispatchEvent(event);
}
}
This is the function that should be called within ngOnInit:
public NewWorkItem(interactionID: unknown, userData: { FirstName: unknown; LastName: unknown }): void {
console.log('NewWorkItem call',interactionID,userData);
const event: Event = new CustomEvent ('wde.newWorkItem', {
detail: {
FirstName: userData.FirstName,
LastName: userData.LastName,
InteractionID: interactionID
},
bubbles: true,
cancelable: true,
composed: false,
});
console.log('NewWorkItem event',event);
window.dispatchEvent(event);
}
Here is where I am facing issues while passing the parameters correctly in ngOnInit:
public ngOnInit(): void {
this.NewWorkItem(this.interactionID, this.userdata );
}
The compiler error I encounter is due to issues with passing the userdata object as a function parameter:
The argument of Typ "Object" cannot be assigned to the parameter of type "{ FirstName: unknown; LastName: unknown; }"
In my attempt to call the function within ngOnInit with the correct parameters, I encountered the following error message: The argument of Typ "Object" cannot be assigned to the parameter of type
"{ FirstName: unknown; LastName: unknown; }"
. I am unsure about the proper way to pass the object as a function parameter.