简体   繁体   English

如何将自定义 JS 文件添加到 Angular 组件

[英]How to add a custom JS file to Angular component

I'm trying to learn Angular and as part of learning, I need to add some JS logic to one of my components which is already in a separate JS file.我正在尝试学习 Angular,作为学习的一部分,我需要向我的一个组件中添加一些 JS 逻辑,该组件已经在一个单独的 JS 文件中。 There is an answer to a very similar question ( How to add custom js file to angular component like css file ).有一个非常相似的问题的答案( How to add custom js file to angular component like css file )。 I did as it recommends, but it looks like something is missing.我按照它的建议做了,但似乎缺少一些东西。 Please help me understand, what did I do wrong?请帮助我理解,我做错了什么?

I created the file custom.js and saved it in the src/assets folder (just trying to see what declaration will work, as I see few different styles):我创建了文件custom.js并将其保存在src/assets文件夹中(只是想看看什么声明会起作用,因为我看到了几种不同的样式):

(function customTestHello1() {
    console.log("customTestHello1: Hello!!!"); 
    alert('customTestHello1: Hello!!!');
})()

function customTestHello2() {
    console.log("customTestHell2: Hello!!!"); 
    alert('customTestHell2: Hello!!!');
}

I included this file in angular.json :我在angular.json包含了这个文件:

"scripts": [ "src/assets/custom.js" ] “脚本”:[“src/assets/custom.js”]

In the ts-file of my component, I added在我的组件的ts-file中,我添加了

declare const customTestHello1: any;
declare const customTestHello2: any;

and in the ngOnInit function of the component added call of these functions并在组件的ngOnInit函数中添加了这些函数的调用

  ngOnInit(): void {
    customTestHello1();
    //customTestHello2();
  }

When I opened my page I saw an error in the console:当我打开我的页面时,我在控制台中看到一个错误:

ERROR ReferenceError: customTestHello1 is not defined
    at MyComponent.ngOnInit (my.component.ts:20)
    at callHook (core.js:3038)
    at callHooks (core.js:3008)
    at executeInitAndCheckHooks (core.js:2960)
    at refreshView (core.js:7186)
    at refreshEmbeddedViews (core.js:8279)
    at refreshView (core.js:7195)
    at refreshComponent (core.js:8325)
    at refreshChildComponents (core.js:6964)
    at refreshView (core.js:7221)

The result is the same even if I uncomment "customTestHello2()".即使我取消注释“customTestHello2()”,结果也是一样的。

What do I do wrong?我做错了什么? How can I embed JS into an Angular project into a component?如何将 JS 嵌入到 Angular 项目的组件中?

If I put following to my script:如果我将以下内容添加到我的脚本中:

window.initMethod = function() { console.log("customInitMethod: Hello!"); }

this to myComponent.ts:这到 myComponent.ts:

let initMethod: any;

and call it:并称之为:

ngOnInit(): void {
    initMethod();
    //customTestHello1();
    //customTestHello2();
  }

I get another error:我收到另一个错误:

core.js:4197 ERROR TypeError: initMethod is not a function at MyComponent.ngOnInit (pingball.component.ts:21) at callHook (core.js:3038) at callHooks (core.js:3008) at executeInitAndCheckHooks (core.js:2960) at refreshView (core.js:7186) at refreshEmbeddedViews (core.js:8279) at refreshView (core.js:7195) at refreshComponent (core.js:8325) at refreshChildComponents (core.js:6964) at refreshView (core.js:7221) core.js:4197 ERROR TypeError: initMethod 不是 MyComponent.ngOnInit (pingball.component.ts:21) at callHook (core.js:3038) at callHooks (core.js:3008) at executeInitAndCheckHooks (core.js) 的函数:2960) at refreshView (core.js:7186) at refreshEmbeddedViews (core.js:8279) at refreshView (core.js:7195) at refreshComponent (core.js:8325) at refreshChildComponents (core.js:6964) at refreshView (core.js:7221)

Please advise!请指教!

Thanks a lot in advance!非常感谢!

PS I would also benefit a lot if you give a good tutorial that does not have an obsolete samples. PS 如果您提供一个没有过时示例的好教程,我也会受益匪浅。

在要使用的组件中导入 custom.js 文件。

Create an angular service to call your customTestHello1() method in your component after importing that service in the respective component.在相应组件中导入该服务后,创建一个 angular 服务以调用组件中的customTestHello1()方法。 Make sure to add service in providers array of module.ts.确保在 module.ts 的 providers 数组中添加服务。 For service creation details you can refer : https://angular.io/tutorial/toh-pt4有关服务创建的详细信息,您可以参考: https : //angular.io/tutorial/toh-pt4

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

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