加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – 如何在不同文件的Angular 2组件中使用javascript

发布时间:2020-12-17 07:53:52 所属栏目:安全 来源:网络整理
导读:我有一个javascript文件,其中包含一些数据操作函数(根本没有DOM操作),例如浮点舍入,数学运算,等等 我的js文件名为myexample.js就是这样 function example(input) { return input * 2}module.exports = { example: example } 然后我有我的角度组件example.com
我有一个javascript文件,其中包含一些数据操作函数(根本没有DOM操作),例如浮点舍入,数学运算,等等

我的js文件名为myexample.js就是这样

function example(input) {
  return input * 2
}
module.exports = { example: example }

然后我有我的角度组件example.component.ts

例如:

import { Component,EventEmitter} from '@angular/core';
@Component({
  selector: 'input-number',template: `<input 
              type='text' 
              [(value)]='value' 
              (blur)='runExample()' 
             />`,inputs: ['value'],outputs: ['valueChange']
})
export class Example {
  value: string;
  valueChange = new EventEmitter;

  runExample() {
    let val = parseFloat(this.value);
    // here i need to call example(input) from myexample.js
    // and assign the return to val
    this.valueChange.emit(val);
  }

我现在已经搜索了很长一段时间并试了很多东西,但遗憾的是没有运气.

如果有人可以提供帮助,我将非常感激.

您可以在TypeScript中导出函数:
export function example(input) {
  return input * 2;
}

并以这种方式使用它(假设您的文件名是lib.ts):

import {example} from './lib';

example();

如果要使用CommonJS文件,则需要在map属性中配置SystemJS:

System.config({
  (...)
  map: {
    lib: 'path/to/lib/js'
  }
});

您可以使用相同的方式导入模块:

import {example} from './lib';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读