Hello All,
Today we will disscus ,how to get Current User Details in Lightning Web Components.
Lightning web component provides us a way to get user information without apex. So to get information about the current user, use the @salesforce/user scoped module.
First we will import user/Id and then we will use it in our code.
getUserCurrentUserInLWC.js
import { LightningElement, wire } from 'lwc';
import getUserInfo from '@salesforce/apex/UserDetails.getUserInfo'; /* Apex method*/
import Id from '@salesforce/user/Id';
export default class LwcUserDetail extends LightningElement {
@wire(getUserInfo, { userId: Id }) // call Apex method and pass User id
userData;
}
Using the wire we call the apex method and then bind the result with userData parameter. So as you can see we get Current User Id in Lightning Web Components using a single line of code.
getUserCurrentUserInLWC.html
Today we will disscus ,how to get Current User Details in Lightning Web Components.
Lightning web component provides us a way to get user information without apex. So to get information about the current user, use the @salesforce/user scoped module.
First we will import user/Id and then we will use it in our code.
getUserCurrentUserInLWC.js
import { LightningElement, wire } from 'lwc';
import getUserInfo from '@salesforce/apex/UserDetails.getUserInfo'; /* Apex method*/
import Id from '@salesforce/user/Id';
export default class LwcUserDetail extends LightningElement {
@wire(getUserInfo, { userId: Id }) // call Apex method and pass User id
userData;
}
Using the wire we call the apex method and then bind the result with userData parameter. So as you can see we get Current User Id in Lightning Web Components using a single line of code.
getUserCurrentUserInLWC.html
<template>
<lightning-card title="User Details in LWC" icon-name="action:user">
<template if:true={userData.data}>
{userData.data.Name}<br/>
{userData.data.Title}<br/>
{userData.data.Profile.Name}
</template>
<template if:true={userData.error}>
{userData.error}
</template>
</lightning-card>
</template>
No comments:
Post a Comment