In assets/dummy-data.js, i have a normal function.
function greetings(){
return {
data: "Greetings from External JS"
}
}In tsconfig.json
"allowJs": true,In angular-cli.json
"scripts": [
"./assets/dummy-data.js"
],If you check the Source tab, scripts.bundle.js, that is already a part of the final bundle.
In whichever component
import { Component, OnInit, AfterViewInit, AfterContentInit } from '@angular/core';
declare var greetings: any; // declare with the name of the function
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
message;
ngOnInit(): void {
this.message = greetings(); // use it like this in any life cycle hook
}
}In html
<h1> Message is {{ message.data }} </h1>