Friday, January 18, 2019

How to import Current User id in Lightning Web Components(lwc)

This post explains the how to import current user id in lightning web components
To import current user id we use the @salesforce/user/Id scoped module.

Syntax

import userId from '@salesforce/user/Id';

Example
LWCCurrentUserIdDemo.html
<template>
    <lightning-card title="CurrentUserId" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            <p>User Id:</p>
            <b>{userId}</b>
        </div>
    </lightning-card>
</template>

LWCCurrentUserIdDemo.js
import { LightningElement } from 'lwc';
import Id from '@salesforce/user/Id';
export default class LWCCurrentUserIdDemo extends LightningElement {
    userId = Id;
}

LWCCurrentUserIdDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="LWCCurrentUserIdDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>
Result

Happy Learning!!!

No comments:

Post a Comment