Typescript 导出函数并将其导入其他文件时出现问题

Typescript 导出函数并将其导入其他文件时出现问题,typescript,Typescript,我有以下文件结构: ---utilities -----index.ts -----tools.ts allfunctions.ts 在文件tools.ts中,我使用export const定义了一些导出的函数。例如: export const helloWorld = () => console.log('Hello World'); 现在,在文件utilities/index.ts中,我导入了该文件,并将其导出如下: import * as toolsFunction from '

我有以下文件结构:

---utilities
-----index.ts
-----tools.ts
allfunctions.ts
在文件
tools.ts
中,我使用
export const
定义了一些导出的函数。例如:

export const helloWorld = () => console.log('Hello World');
现在,在文件
utilities/index.ts
中,我导入了该文件,并将其导出如下:

import * as toolsFunction from './tools';

export { toolsFunction }
import { taskFunctions as Utility } from './utilities/';

taskFunctions.helloWorld();
现在,在我的
allfunctions.ts
中,我使用的
helloWorld
函数如下:

import * as toolsFunction from './tools';

export { toolsFunction }
import { taskFunctions as Utility } from './utilities/';

taskFunctions.helloWorld();
在我编译代码之前,它工作得很好。在编译过程中,我遇到以下错误:

TypeError: Cannot read property 'helloWorld' of undefined

我做错了什么?

当您使用import导入typescript中的内容时,这就是语法

import * as myModule from './superModule'
myModule.doStuff()

在这里,它中断是因为任务函数不存在,您应该使用实用工具调用helloWorld函数。 尝试:


有关更多信息,您还可以检查这是因为您没有使用正确的语法调用
helloWorld()
函数。您已将其导出为
toolsFunction
,但您将其导入为
allfunctions.ts中的
taskFunctions
。此外,您还需要基于新的别名
实用程序
调用该函数。因此,您的代码应该如下所示

工具。ts

export const helloWorld = () => console.log('Hello World');
import * as toolsFunction from './tools';

export { toolsFunction }
import {toolsFunction as Utility} from './utilities/'

export {Utility};
import { Component } from '@angular/core';
import {Utility} from './allfunctions'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 4';

  constructor(){
    Utility.helloWorld();
  }
}
index.ts

export const helloWorld = () => console.log('Hello World');
import * as toolsFunction from './tools';

export { toolsFunction }
import {toolsFunction as Utility} from './utilities/'

export {Utility};
import { Component } from '@angular/core';
import {Utility} from './allfunctions'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 4';

  constructor(){
    Utility.helloWorld();
  }
}
所有函数.ts

export const helloWorld = () => console.log('Hello World');
import * as toolsFunction from './tools';

export { toolsFunction }
import {toolsFunction as Utility} from './utilities/'

export {Utility};
import { Component } from '@angular/core';
import {Utility} from './allfunctions'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 4';

  constructor(){
    Utility.helloWorld();
  }
}
在app.component.ts中的使用

export const helloWorld = () => console.log('Hello World');
import * as toolsFunction from './tools';

export { toolsFunction }
import {toolsFunction as Utility} from './utilities/'

export {Utility};
import { Component } from '@angular/core';
import {Utility} from './allfunctions'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 4';

  constructor(){
    Utility.helloWorld();
  }
}
工作演示: