![](/img/trans.png)
[英]Typescript and NodeJS : ReferenceError : Module is not defined
[英]“ReferenceError: module is not defined” when evaluating typescript statement
這是我的代碼
// properties.ts
export const properties = {
title: "Google"
};
// example.ts
import { properties } from './properties.ts';
console.log(properties.title); // Prints Google
console.log(eval("properties.title")); // Expected to print Google but throws ReferenceError: properties is not defined
但是,console.log(eval('properties_1.properties.title'))//打印Google
但是,如何獲得“ properties_1”是我的關注。
TS中的import語句將轉換為新變量。 默認情況下,這是打字稿中的內容,而eval無法對其進行計算 。
我嘗試過這種方法,但效果很好,
import { properties } from './properties';
let p = properties;
console.log(p.title); // Prints Google
console.log(eval('p.title'));
您可以通過另一種方式將屬性導入變量,
import * as properties from './properties';
console.log(properties.properties.title); // Prints Google
console.log(eval('properties.properties.title')); // Prints Google
確保以這種方式進行編譯,
>tsc dynamic.ts -t ES6 -m commonjs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.