Lightning Web Components (LWC) are made up of multiple files in a bundle. For this question I am focusing on the component's main .js file where the web component js class is implemented.
Example code:
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import doSomething from '@salesforce/apex/MyApexClass.doSomething';
export default class MyLWC extends NavigationMixin(LightningElement) {
@api a;
@api b;
download() {
let that = this;
doSomething({
param1: this.a,
param2: this.b
})
.then((result) => {
that.navigateToWebPage(result);
})
.catch((error) =>{
console.log(`Error occured ${error}`);
});
}
navigateToWebPage(url) {
// Navigate to a URL
this[NavigationMixin.Navigate]({
type: 'standard__webPage',
attributes: {
url: url
}
},
false // Replaces the current page in your browser history with the URL
);
}
}
There are two things I don't understand here:
extends NavigationMixin(LightningElement)
syntax - why brackets? Can js classes extend more than one parent class?this[NavigationMixin.Navigate]
- judging by the code it seems like'this'
got assigned the Navigate function (and potentially other functions from NavigateMixin?) to its prototype. Why aren't we using NavigationMixin like any other imported component - just calling their functions directly, not throughthis
?
Can someone explain what is actually happening when using this kind of class extensions/inheritance, and if the syntax used here is something standard from the web components world or something specific for LWC?
Thanks for your answers!