简体   繁体   English

将js文件导入ts文件

[英]importing js file into ts file

I've already read several posts concerning this. 我已经阅读了有关此的几篇文章。 My approach was working yesterday but today it is failing. 我的方法昨天有效,但今天却失败了。 I'm importing a js file into a ts file. 我正在将js文件导入ts文件。 allowJs is set to true in tsconfig.json . allowJs设置为truetsconfig.json

Getting the following error: 出现以下错误:

ERROR in src/app/core/input-parser.service.ts(5,26): error TS2497: Module '".../dashboard/shared/models/ecn-info"' resolves to a non-module entity and cannot be imported using this construct.

input-parser.service.ts: 输入parser.service.ts:

import * as EcnInfo from '../../../../shared/models/ecn-info';

ecn-info.js: ECN-info.js:

class EcnInfo {
  constructor(name, structure, status) {
    this.name = name;
    this.structure = structure;
    this.status = status;
  }
}

module.exports = EcnInfo;
  1. Use an export default in your ecn-info.js file: ecn-info.js文件中使用export default
export default class EcnInfo {
    constructor(name, structure, status) {
        this.name = name;
        this.structure = structure;
        this.status = status;
    }
}
  1. Disable noImplicitAny in your tsconfig.json : 禁用noImplicitAnytsconfig.json
{
  "compilerOptions": {
    "noImplicitAny": false
    ...
  }
}
  1. Import like this: 像这样导入:
import EcnInfo from '../../../../shared/models/ecn-info';

Update 更新

Here is an approach without using default export: 这是一种不使用默认导出的方法:

  1. Use an export in your ecn-info.js file: ecn-info.js文件中使用export
export class EcnInfo {
    constructor(name, structure, status) {
        this.name = name;
        this.structure = structure;
        this.status = status;
    }
}
  1. Disable noImplicitAny in your tsconfig.json : 禁用noImplicitAnytsconfig.json
{
  "compilerOptions": {
    "noImplicitAny": false
    ...
  }
}
  1. Import like this: 像这样导入:
import {EcnInfo} from '../../../../shared/models/ecn-info';

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

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