How to call external javascript function in angular 5?

前端 未结 4 1542

I have downloaded a theme from this link. I have to define script and CSS in index.html file.

index.html (body section)


  

        
4条回答
  •  攒了一身酷
    2020-12-17 05:51

    You can use javascript in the Angular application.

    Step 1. Create demo.js file in assets/javascript folder.

    export function test1(){
        console.log('Calling test 1 function');
    }
    

    Step 2. Create demo.d.ts file in assets/javascript folder.

    export declare function test1();
    

    Step 3. Use it in your component

    import { test1 } from '../assets/javascript/demo'; 
     @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      constructor() {
        console.log(test1());
      }
    }
    

    Note: js and .d.ts file name should be same

提交回复
热议问题