简体   繁体   English

Angular 8 项目中的外部 .js 文件

[英]External .js file in an Angular 8 project

I'm trying to call a function in a .js file from an Angular component, but I get the error "ERROR ReferenceError: myTest is not defined at TechnologiesComponent.onClick".我正在尝试从 Angular 组件调用 .js 文件中的函数,但出现错误“错误 ReferenceError:myTest 未在 TechnologiesComponent.onClick 中定义”。

I have followed the steps described here , so I have created a file called custom.js in my src folder.我已按照此处描述的步骤进行操作,因此我在我的 src 文件夹中创建了一个名为 custom.js 的文件。 截屏

The file contains the following:该文件包含以下内容:

function myTest() {
  alert('Welcome to custom js');
}

$(function() {
  alert('Hello, custom js');
});

I have added the script in the scripts array in my angular.json, like so:我在 angular.json 的脚本数组中添加了脚本,如下所示:

        "tsConfig": "tsconfig.app.json",
        "aot": false,
        "assets": [
          "src/assets"
        ],
        "styles": [
          "src/styles.css"
        ],
        "scripts": ["src/custom.js"]
      },

The .ts file where I want to use the .js file looks like this:我想在其中使用 .js 文件的 .ts 文件如下所示:

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

declare const myTest: any;

@Component({
  selector: 'app-technologies',
  templateUrl: './technologies.component.html',
  styleUrls: ['./technologies.component.css']
})
export class TechnologiesComponent {
  onClick() {
    myTest();
  }
}

A button is added to my template: Click Me我的模板中添加了一个按钮:单击我

When the button is pressed, the error "ERROR ReferenceError: myTest is not defined at TechnologiesComponent.onClick" is thrown.按下按钮时,会抛出错误“ERROR ReferenceError: myTest is not defined at TechnologiesComponent.onClick”。 Why?为什么?

declare your function as a variable (in JS both are same)将您的函数声明为变量(在 JS 中两者相同)

custom.js自定义.js

myTest = function(){
   alert('Welcome to custom js');
}

also, verify if the script is getting imported in the final build另外,验证脚本是否在最终构建中被导入

you may need to re-build if you are using ng serve如果您使用ng serve ,您可能需要重新构建

If you want to use code from .js files in .ts files you should use export and import .如果你想在 .ts 文件中使用来自 .js 文件的代码,你应该使用exportimport Example with your project:您的项目示例:

custom.js自定义.js

export function myTest() {
  alert('Welcome to custom js');
}

app.component.ts app.component.ts

import {Component} from '@angular/core';
import * as custom from 'src/custom.js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  onClick() {
    custom.myTest();
  }
}

take note of line import * as custom from 'src/custom.js';注意 line import * as custom from 'src/custom.js'; It's one of the best ways to import smth from non-ts files.这是从非 ts 文件导入 smth 的最佳方法之一。

Structure:结构:

项目结构

I am using angular 10 and trying to access plane js file and It's not allow to change js file ie can't able to add export in it我正在使用 angular 10 并尝试访问平面 js 文件,并且不允许更改 js 文件,即无法在其中添加export

  1. add your js files in angular.jsonangular.json中添加你的 js 文件

在此处输入图像描述

  1. I want to access print() function in angular from myJs.js and which is like我想从myJs.js以角度访问print()函数,就像

 // MyJS.js function print(a){ cosole.log(a +" Code from JS"); }

  1. app.component.ts app.component.ts

 import {Component} from '@angular/core'; // following variable name and function name in js file should be same declare var print: any; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { onClick() { print('Yes'); } }

  1. and fire ng serve again然后再次开火

( If still not work for you then try to put your js file in assets folder and change the path in angular.json ) (如果仍然不适合您,请尝试将您的 js 文件放入assets文件夹并更改angular.json中的路径)

Find out the answer here https://www.c-sharpcorner.com/article/using-external-js-file-in-angular-app/在这里找到答案https://www.c-sharpcorner.com/article/using-external-js-file-in-angular-app/

Great explanation.很好的解释。 Thanks to rohol amin.感谢 rohol amin。

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

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