Sample CODE 1 :
import { Component,OnInit } from '@angular/core';
import {exampleClass} from './exampleClass'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
list: number[] = [1, 2, 3];
constructor(a:exampleClass) {
a.hello();
}
}
Example CODE 2 :
import { Component,OnInit } from '@angular/core';
import {exampleClass} from './exampleClass'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
list: number[] = [1, 2, 3];
constructor() {
const a = new exampleClass ;
}
}
exampleClass.ts Code :
export class exampleClass {
hello(){
console.log("A");
}
}
Can you explain why code 1 results in an error while printing A in the console and what is the difference between these two initialization methods?