This is a basic javascript question. FYI the context is: It is in a Javascript controller for a Salesforce Lightning Web Component, editing in Visual Studio. The code is for selecting a set of table column definitions to render as a lightning-datatable. (I will want to be able to select which columns to display in order to reuse this for different reports.)
The following very short javascript controller code throws these errors. I removed all other code to simplify and make it reproducible, so this is the entire code in the file.
- LWC1007: /Users/xxx/dev/xxx/xxx/xxx/MyClass/jsbugtest.js: Unexpected token, expected "," [12, 26]
- ts(1005) ‘,’ expected. [12, 26]
- ts(1005) ‘;’ expected. [12, 26]
- ts(1128) Declaration or statement expected. [16, 1]
If I comment out lines 12-14 and uncomment line 10, the errors disappear and Salesforce accepts the upload.
I'd appreciate if you can tell me why the if statement does not work and where there is language documentation explaining it if possible. Thank you!
import {LightningElement, api, wire, track} from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class MyClass extends NavigationMixin(LightningElement) {
generalReportName = 'general';
complianceReportName = 'compliance';
activeReportName = 'compliance';
searchResultsColumns = [{label: 'Sales', fieldName: 'Record'}];
complCols = [{label: 'Compliance', fieldName: 'Record'}];
//searchResultsColumns = (activeReportName == complianceReportName) ? searchResultsColumns : complCols; // line 10
if (activeReportName != complianceReportName) { // line 12
searchResultsColumns = complCols;
}
} // line 16