typescript 如何在javascript中导入打字稿类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45349497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:45:53  来源:igfitidea点击:

How to import a typescript class in javascript?

javascripttypescript

提问by user2301

This is how I import app.config.tsclass in typescript and use it. How do I import the same AppConfigclass in Javascript file (config.js) and access the method getConfig?

这就是我在打字稿中导入app.config.ts类并使用它的方式。如何AppConfig在 Javascript 文件 (config.js) 中导入相同的类并访问该方法getConfig

service.ts

服务.ts

import { AppConfig } from '../app.config';
constructor(private http: Http, private config: AppConfig) {
    this.dataURL = this.config.getConfig('restApi') + '/api/getdata/'
    console.log(this.dataURL)
}

采纳答案by Michael Fedora

I don't know exactly what you are asking, so I've split it into to parts:

我不知道你在问什么,所以我把它分成了几个部分:

If you are trying to import a Typescript file into a JavaScript file

如果您尝试将 Typescript 文件导入 JavaScript 文件

The best solution would be to rename your config.js to config.ts and treat it like a typescript file.

最好的解决方案是将您的 config.js 重命名为 config.ts 并将其视为打字稿文件。

i.e. if you have this:

即如果你有这个:

export class AppConfig {
  foo: string;
  bar: number;
}

And in your config.js you have this:

在你的 config.js 你有这个:

export const Config = {
  foo: 'Hello World',
  bar: 42
}

but I assume you want something akin to:

但我假设你想要类似的东西:

import { AppConfig } from './app.config'

export const Config: AppConfig = {
  foo: 'Hello World',
  bar: 42
}

Then just rename config.js to config.ts. It has to be a typescript file for type checking anyways (unless you put the allowJs rule in the tsconfig.json, but even then I don't think it's smart to import .ts files into .js files).

然后只需将 config.js 重命名为 config.ts。无论如何,它必须是用于类型检查的打字稿文件(除非您将 allowJs 规则放在 tsconfig.json 中,但即便如此,我认为将 .ts 文件导入 .js 文件也不明智)。

If you are trying to import a Javascript file (or something) dynamically from your URL

如果您尝试从您的 URL 动态导入 Javascript 文件(或其他内容)

Importing JavaScript files dynamically doesn't actually work in the browser for a couple of technical reasons, not the least of which is security, but also because ES6's "import" (or node's "require") is not in the browser standard.

由于一些技术原因,动态导入 JavaScript 文件实际上在浏览器中不起作用,其中最重要的是安全性,还因为 ES6 的“导入”(或节点的“要求”)不在浏览器标准中。

Instead, I recommend you convert your config to a JSONfile and use JavaScript's native JSON.parseto parse it. I.E:

相反,我建议您将配置转换为JSON文件并使用 JavaScript 的本机JSON.parse来解析它。IE:

http.get(this.dataUrl).toPromise().then(response => {
  let config = null;
  try {
    config = JSON.parse(response.data);
  } catch (e) {
    console.error("Couldn't parse response!");
  }
  this.config = new AppConfig(config); // See note
}

Or something along those lines.

或类似的规定。

Note:I assume you have both a copy and a default constructor for your AppConfig class. If you don't, then you can create one, create a parse-function or something akin to that.

注意:我假设您的 AppConfig 类有一个副本和一个默认构造函数。如果你不这样做,那么你可以创建一个,创建一个解析函数或类似的东西。