简体   繁体   English

添加Javascript或将js文件导入Angular 9组件

[英]Adding Javascript or importing js file into Angular 9 component

I'm new to Angular and creating a project that that uses routing.我是 Angular 的新手,我正在创建一个使用路由的项目。 I'd like to import a js file from src/assets/js/custom.js我想从 src/assets/js/custom.js 导入一个 js 文件

I've created a service that imports an injectable like so我创建了一个像这样导入可注射的服务

test.service.ts测试服务.ts

import { Injectable } from '@angular/core';

@Injectable()
export class TestService {
    testFunction() {
      console.log('Test');
    }
}

home.compontents.ts looks like home.components.ts 看起来像

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { TestService } from './test.service';

declare var require: any

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
  providers: [TestService]

})

export class HomeComponent implements OnInit {

  constructor() {private testService: TestService}

  ngOnInit(): void {
    this.testService.testFunction();

  }

}

But I am getting the following error但我收到以下错误


17   constructor() {private testService: TestService}
                    ~~~~~~~
src/app/home/home.component.ts:19:13 - error TS1005: ';' expected.

19   ngOnInit(): void {
               ~
src/app/home/home.component.ts:20:9 - error TS1005: ':' expected.

20     this.testService.testFunction();
           ~
src/app/home/home.component.ts:20:36 - error TS1005: ',' expected.

20     this.testService.testFunction();
                                      ~
src/app/home/home.component.ts:24:1 - error TS1128: Declaration or statement expected.

24 }
   ~

I've tried so many different ways from Google searches and not coming up with anything.我从谷歌搜索中尝试了很多不同的方法,但没有提出任何建议。 Can anyone please help?有人可以帮忙吗?

UPDATE更新

Thanks for the updates, I've updated the constructor, however I am getting an error感谢您的更新,我已经更新了构造函数,但是我收到了一个错误

ERROR in src/app/home/home.component.ts:3:29 - error TS2307: Cannot find module './test.service' or its corresponding type declarations.

3 import { TestService } from './test.service';

I'm not sure if I am going the right way with this.我不确定我是否走对了这条路。 Each component I am using has 1 or 2 js files that I need to import.我使用的每个组件都有 1 或 2 个需要导入的 js 文件。 What would be the best way to do this?最好的方法是什么?

A service passed as a parameter in class constructor to be injected as a dependency.在 class 构造函数中作为参数传递的服务作为依赖项注入。

constructor(private testService: TestService) {}

The constructor is written incorrectly.构造函数写错了。 Please write it as given below请按如下所示写

 constructor(private testService: TestService) {}

Also, you have given service as @Injectable() ,then you have to define the service in app.module.ts file.此外,您已将服务提供为@Injectable() ,然后您必须在app.module.ts文件中定义服务。 Alternatively, you can give或者,您可以给

@Injectable({
  providedIn: 'root'
})

This will eliminate adding the service in providers.这将消除在提供程序中添加服务。

home.compontents.ts constructor should be like below: home.compontents.ts 构造函数应该如下所示:

constructor(private testService: TestService) {}

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

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