如何在角度5中调用外部javascript函数? [英] How to call external javascript function in angular 5?

查看:83
本文介绍了如何在角度5中调用外部javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个主题下载了一个主题链接。我必须在index.html文件中定义脚本和CSS。



index.html(正文部分)

 <身体GT; 
< app-root>< / app-root>
< script type =text / javascriptsrc =./ assets / js / main.85741bff.js>< / script>
< script type =text / javascriptsrc =./ assets / js / common.js>< / script>
< / body>

我已经在common.js中定义了我的函数,并从main.85741bff.js文件中调用它。 / p>

common.js(function)

  document.addEventListener(DOMContentLoaded ,function(event){
masonryBuild();
navbarToggleSidebar();
navActivePage();
});

问题是我可以在页面重新加载时调用该函数但无法调用该函数内容加载。



任何人都可以帮我解决这个问题吗?任何帮助将不胜感激。

解决方案

您可以在Angular应用程序中使用javascript。



步骤1.在 assets / javascript 文件夹中创建 demo.js 文件。

 导出函数test1(){
console.log('调用测试1函数');
}

步骤2.创建 demo.d.ts 文件在 assets / javascript 文件夹中。

  export declare function test1(); 

步骤3.在组件中使用

 从'../assets/javascript/demo'导入{test1}; 
@Component({
selector:'app-root',
templateUrl:'。/ app.component.html',
styleUrls:['。/ app.component。 css']
})
导出类AppComponent {
constructor(){
console.log(test1());
}
}

注意:js和.d.ts文件名应相同


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

index.html (body section)

<body>
  <app-root></app-root>
  <script type="text/javascript" src="./assets/js/main.85741bff.js"></script>
  <script type="text/javascript" src="./assets/js/common.js"></script>
</body>

I have defind my function in common.js and call it from main.85741bff.js file.

common.js (function)

document.addEventListener("DOMContentLoaded", function (event) {
     masonryBuild();
     navbarToggleSidebar();
     navActivePage();
});

The issue is that I am able to call the function while page reloads but can't call the function while content load.

Can anyone help me to resolve this issue? Any help would be appreciated.

解决方案

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

这篇关于如何在角度5中调用外部javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆