我想要做的是将一个对象从我的typescript文件导入到一个脚本标记中
这就是我正在尝试做的事情
<div id="app-root"></div>
<script>
import {config} from '../config.ts';
let script = document.createElement('script');
script.src = `https://maps.googleapis.com/maps/api/js?key=${config.GOOGLE_MAPS_KEY}`;
document.getElementsByTagName('script')[0].parentNode.appendChild(script);
</script>
我不确定这是否可能,但我的react应用程序正在# app -root中呈现,我正在尝试处理我的配置中的API密钥。当我运行这段代码时,什么也没有发生,但是如果我添加了静态值,它似乎可以工作。
我期望发生的是,通过将变量传递给脚本src,脚本将成功执行。
发布于 2021-01-09 02:15:38
需要将typescript文件编译/转换为JS ( .js文件),以便在浏览器中使用。
您可以在以下位置在线编译TS文件:https://www.typescriptlang.org/play并将输出复制到.js中
或
通过命令行安装它(假设您在Windows上)
npm install typescript // add -g if you want to install it globally
cd intoYourFolder // change directory to the folder where you have the .ts file
tsc *.ts // this will compile all typescript files in the directory
tsc yourFile.ts // this will compile only a specific file
https://stackoverflow.com/questions/65633621
复制相似问题