简体   繁体   English

有没有办法在javascript文件中要求打字稿文件?

[英]Is there a way to require a typescript file inside a javascript file?

I created a project using ng .我使用ng创建了一个项目。 I called ng eject to get a webpack.config.js.我调用ng eject来获取 webpack.config.js。 I added pug to the webpack.我在 webpack 中添加了 pug。 I want to pass the data from src/environments/environment.ts to pug but I can't figure out how to require the typescript file from a normal javascript file.我想将数据从 src/environments/environment.ts 传递给 pug,但我不知道如何从普通的 javascript 文件中获取 typescript 文件。

I can always change environment.ts to a json file or javascript file but would like to leave it the way it is.我总是可以将 environment.ts 更改为 json 文件或 javascript 文件,但希望保持原样。

Here is the environment.ts file这是 environment.ts 文件

export const environment = {
  production: false,
  title: 'My App DEV'
};

As long as it is actually valid JavaScript (not TypeScript), as your example is, then yes.只要它实际上是有效的 JavaScript(不是 TypeScript),就像你的例子一样,那么是的。 You'll just need to include/require it including the extensions:您只需要包含/要求它包括扩展:

require('./environment.ts');
// or
import environment from './environment.ts';

If it were to contain TypeScript code, you'd need to transpile it to regular JavaScript at some point.如果它包含 TypeScript 代码,您需要在某个时候将其转换为常规 JavaScript。 Where/when/how you do that would be up to your build pipeline.您在哪里/何时/如何做这取决于您的构建管道。

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

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