简体   繁体   English

从Angular 5调用外部js函数不起作用

[英]Calling external js function from Angular 5 not working

I called hello function from ts file but I got error like hello is not defined. 我从ts文件中调用了hello函数,但出现错误,例如hello未定义。

hello.js hello.js

function hello() {
    alert("Hello.......!");
}

index.html index.html

<script src="./assets/js/hello.js"></script>

component.ts component.ts

declare var hello: any;

// calling on load
let result = hello();

console.log(result);

Include your JS file in angular-cli.json file under scripts array 将您的JS文件包含在scripts数组下的angular-cli.json文件中

"scripts": [

]

make sure to put the correct relative URL. 确保输入正确的相对URL。 And then you can simply use it in your ts file. 然后,您可以简单地在ts文件中使用它。 Make sure to restart ng serve if you are using it. 确保重新启动ng serve ,如果您正在使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM