If an LWC makes a default export for its component, plus an additional export, then template.querySelectorAll('div') (or any selection) is empty. Removing the additional export removes this bug.
Using this template:
<template>
<div>what in the world</div>
</template>
And this JavaScript:
import { LightningElement, api } from "lwc";
// THIS IS THE MONEY LINE
export const t1 = 123;
export default class MyComponent extends LightningElement {
@api reportValidity() {
console.log('div count', this.template.querySelectorAll('div').length);
}
renderedCallback() {
console.log('div count', this.template.querySelectorAll('div').length);
}
}
The query selector will always return 0 results, no matter what.
If you remove the export const t1 line (so there is only the default export) this problem disappears.
You can see the diff between the compiled versions here: https://www.diffchecker.com/iFhvJQBF
I tried to repro this in the playground but couldn't: https://developer.salesforce.com/docs/component-library/tools/playground/ol0g50Fk0/1/edit
However I don't think it's any issue in our org, as it's a pretty simple dev org.
The query selector is not empty.
The query selector is empty.
Chrome 81 and probably more.
Not sure, this is in Salesforce, API version 48.
Error while creating work item!
After debugging this looks like a locker issue.
/cc @caridy
Error while creating work item!
Afternoon! Just writing to say that this is still an issue in API version 50. FYI, no work item has been created for this.
This issue has been linked to a new work item: W-8386672