Typescript 如何修复';没有导出的成员';

Typescript 如何修复';没有导出的成员';,typescript,Typescript,创建自己的函数,但当我导入在另一个文件中创建的模块时,当我已经导出函数所在的类时,它将出错 我已经试过很多和我类似的问题,但似乎不起作用。。。也许这只是我的开发环境 chiFunctions.ts export class ChiFunctions { public Arrivederci() { const time: moment().format('MMMM Do YYYY, h:mm:ss a');

创建自己的函数,但当我导入在另一个文件中创建的模块时,当我已经导出函数所在的类时,它将出错

我已经试过很多和我类似的问题,但似乎不起作用。。。也许这只是我的开发环境

chiFunctions.ts

    export class ChiFunctions {
            public Arrivederci() {
                    const time: moment().format('MMMM Do YYYY, h:mm:ss a');
                    let statements: string[] = ['good-night', 'Good Morning','Good-afternoon'];

                    if (time > '12:00:00 pm')
                            return statements[1];
                    else if (time < '12:00:00 pm')
                            return statements[2];
            }
    }
    import { Arrivederci } from 'utils/chiFunctions'; // Here is the error
应该发生什么

console.log(`${Arrivederci}`);
输出


您的代码有两个问题:

  • 导出的是类,而不是函数
  • 您实际上并没有调用该函数
要使当前代码正常工作,您需要执行以下操作:

import { ChiFunctions } from 'utils/chiFunctions';
const functions = new ChiFunctions();
console.log(`${functions.Arrivederci()}`);
您可以使用一个更简洁的函数,它可以在不创建
ChiFunctions
类的实例的情况下调用,但这仍然有点混乱

虽然在C#和Java等语言中,所有内容都必须封装在类中,但JavaScript/TypeScript中没有这样的限制-您只需从
chiFunctions
文件导出函数,就可以删除很多样板文件:

export function Arrivederci() {
    const time: moment().format('MMMM Do YYYY, h:mm:ss a');
    let statements: string[] = ['good-night', 'Good Morning','Good-afternoon'];

    if (time > '12:00:00 pm')
        return statements[1];
    else if (time < '12:00:00 pm')
        return statements[2];
}

这几乎完全是您的原始代码,只是修改了语法,以便调用函数

您的代码有两个问题:

  • 导出的是类,而不是函数
  • 您实际上并没有调用该函数
要使当前代码正常工作,您需要执行以下操作:

import { ChiFunctions } from 'utils/chiFunctions';
const functions = new ChiFunctions();
console.log(`${functions.Arrivederci()}`);
您可以使用一个更简洁的函数,它可以在不创建
ChiFunctions
类的实例的情况下调用,但这仍然有点混乱

虽然在C#和Java等语言中,所有内容都必须封装在类中,但JavaScript/TypeScript中没有这样的限制-您只需从
chiFunctions
文件导出函数,就可以删除很多样板文件:

export function Arrivederci() {
    const time: moment().format('MMMM Do YYYY, h:mm:ss a');
    let statements: string[] = ['good-night', 'Good Morning','Good-afternoon'];

    if (time > '12:00:00 pm')
        return statements[1];
    else if (time < '12:00:00 pm')
        return statements[2];
}
这几乎完全是您的原始代码,只是修改了语法,以便调用函数