简体   繁体   English

如何在angular component.ts文件中访问JS文件api

[英]How to access JS file api in angular component.ts file

I have one function in js file under src/server/js/controller.js, and I want to use that API inside component's ts file. 我在src / server / js / controller.js下的js文件中有一个函数,我想在组件的ts文件中使用该API。 I wrote the following code to do same but seems not working 我写了下面的代码做同样的事情,但似乎不起作用

controller.js controller.js

const getDomainName= (hostName) => {
    console.log('hostName get controller', hostName); 
}
module.exports = {getDomainName};

flush-component.ts flush-component.ts

import {getDomainName} from '../../../../server/controllers/controller.js';
@Injectable({
    providedIn: 'root'
})

export class LaunchUtil {
   //How to call from here
}

Note: I should not put this file under assets folder since it is referring by other JS files. 注意:我不应该将此文件放在Assets文件夹下,因为它是由其他JS文件引用的。

You should export it like this: 您应该这样导出它:

const getDomainName= (hostName) => {
    console.log('hostName get controller', hostName); 
}
export { getDomainName }

And use it anywhere in your application project using import syntax: 并使用import语法在应用程序项目中的任何位置使用它:

import { getDomainName } from '../../../../server/controllers/controller.js';

暂无
暂无

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

相关问题 无法将 js 文件导入 component.ts - Could not import js file into component.ts 如何在 Angular (component.ts) 中使用 JS function - How to use JS function in Angular (component.ts) Collapsible Sidebar Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file? 如何在 HTML 中获取输入类型 Date 并在 angular7 component.ts 文件中使用? - How to take input type Date in HTML and use in angular7 component.ts file? 如何在angular的component.ts文件中传递复选框的选中和未选中值? - How to pass the checked and unchecked value of checkboxes in component.ts file in angular? 如何将index.html页面中的值传递给angular4中的component.ts文件 - How to pass a value from index.html page to component.ts file in angular 4 如何正确地将函数从 component.ts 移动到服务文件? - How to properly move function from component.ts to a service file? 如何将 Java 脚本文件插入角度 component.ts 类型脚本文件并执行 MEAN 堆栈的 onClick 函数 - How to insert Java-script file into a angular component.ts Type-script file and execute the onClick function for MEAN stack 来自component.ts的* ngFor访问元素 - Access element of *ngFor from component.ts 如何在发射成功时处理从 service.ts 到 component.ts 的 socket.io 确认 Angular 8 - How to handle socket.io acknowledgement from service.ts to component.ts on emit success Angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM