Call pure javascript function from Angular 2 component

I have one file that have dozens javascript functions. What I want to do is to import that file into Angular 2 component and run init() function that is defined in "external.js" file.

import { Component, AfterViewInit } from '@angular/core';

import "../../../../assets/external.js";

    selector: 'app-component',
    templateUrl: 'app.component.html'
export class ComponentNameComponent implements AfterViewInit {
    constructor() { }

    ngAfterViewInit(): void {
      // invoke init() function from external.js file

external.js is loaded using import and in ngAfterViewInit() I want to invoke init() function that is in external.js that calls all other methods in that external file.

Here is part of external.js:

function init() {



You can import and declare your external object. after then you can use it in your component.

import 'external.js'
declare var myExtObject: any;

I made an example in plunker:

Hope this helps.


Checkout my git project of angular 6 - I used this in login component -


define(["require", "exports"], function(require, exports){
   exports.value = "aaa";
   exports.gajender = function(){console.log(2)}; 


 import * as MyModule from './exter.js';

 console.log('my value is', MyModule.gajender());

In tsconfig Add

"allowJs": true


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.