Access control

Apex uses visibility keywords to control what classes, properties, and methods are visible within the scope of the class, within a package, and outside it. The same level of control is available for the Lightning components you place in your package.

JavaScript methods and properties within the controller are private by default. You can, however, add the @api annotation to enable access by other components in your package namespace. In the example shown here, the race component is used to display information about a given race and receives the information to display via properties:

import { LightningElement, api } from 'lwc';

export default class Race extends LightningElement {
// Public properties
@api
raceId;
@api
name;
@api
raceDate;
@api

completed;
// ...

These properties can be referenced programmatically or via HTML. The raceCalendar component references these properties in its markup as shown:

<template for:each={calendar.data} for:item="race">
<ul class="slds-has-dividers_bottom-space" key={race.Id}>
<c-race
race-id={race.Id}
name={race.Name}
race-date={race.RaceDate}
completed={race.Completed}
location={race.Location}
selected={race.Selected}
onselect={handleSelect}>
</c-race>
</ul>
</template>
As with Apex, it is actually best practice to mark something as having the lowest level of access unless you have good reason to increase it. As such, the samples in this book mostly utilize private attributes, as these represent the internal state of the component and should not be accessible even to any other components, including those within the package. As with general Apex coding, this best practice helps manage coupling and allows for greater factoring freedom.

The Making components customizable section will explain additional ways to expose your components to Lightning platform tools such as Lightning App Builder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.96.146