簡體   English   中英

將js文件導入ts文件

[英]importing js file into ts file

我已經閱讀了有關此的幾篇文章。 我的方法昨天有效,但今天卻失敗了。 我正在將js文件導入ts文件。 allowJs設置為truetsconfig.json

出現以下錯誤:

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.

輸入parser.service.ts:

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

ECN-info.js:

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

module.exports = EcnInfo;
  1. ecn-info.js文件中使用export default
export default class EcnInfo {
    constructor(name, structure, status) {
        this.name = name;
        this.structure = structure;
        this.status = status;
    }
}
  1. 禁用noImplicitAnytsconfig.json
{
  "compilerOptions": {
    "noImplicitAny": false
    ...
  }
}
  1. 像這樣導入:
import EcnInfo from '../../../../shared/models/ecn-info';

更新

這是一種不使用默認導出的方法:

  1. ecn-info.js文件中使用export
export class EcnInfo {
    constructor(name, structure, status) {
        this.name = name;
        this.structure = structure;
        this.status = status;
    }
}
  1. 禁用noImplicitAnytsconfig.json
{
  "compilerOptions": {
    "noImplicitAny": false
    ...
  }
}
  1. 像這樣導入:
import {EcnInfo} from '../../../../shared/models/ecn-info';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM