Coming from the realm of React, I am well-versed in the fundamental concepts of launching an application with an index.js
, avoiding direct involvement with HTML, and utilizing import
and export
statements to share views among different JavaScript files. In React, export default
designates the default import object, while export
allows for importing multiple objects from a single file.
Given this background, how different is Angular really?
In an attempt to familiarize myself with Angular, I decided to start with a simple task of creating a logging class. It seemed straightforward, at least in the React context. Here's how I would have approached it:
import {Injectable} from '@angular/core';
@Injectable()
class LoggerService {
info(msg: any) {
console.log(msg);
}
warn(msg: any) {
console.warn(msg);
}
error(msg: any) {
console.error(msg);
}
}
export LoggerService;
However, upon trying this, I encountered a warning from TSLint:
TSLint: unused expression, expected an assignment or function call...
Is it not allowed to define classes before exporting them in Angular?
I've noticed that most tutorials suggest keeping everything within the export class, which is different from what I'm accustomed to. If adhering to this practice is the Angular way, then I'll follow it.
TLDR: Why am I receiving an error with the code above?