评估打字稿语句时出现“ ReferenceError:模块未定义”

时间:2019-03-22 06:09:51

标签: typescript

这是我的代码

// 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”。

1 个答案:

答案 0 :(得分:2)

TS中的import语句将转换为新变量。默认情况下,这是打字稿中的内容,评估不能计算

我尝试过这种方法,并且有效,

    // Not empty, array of numerous elements, just for example
    // and imagination of array here.
    $items = [];
    $variable =  
         '<soap:Envelope>
              <soap:Header>
                  <F2SoapHeader>
                      <login>login</login>
                      <psw>psw</psw>
                  </F2SoapHeader>
              </soap:Header>
              <soap:Body>
                  <method>
                      <items>
                          <?php foreach ($items as $item){ ?>
                              <item>
                                  '. $item .' 
                              </item>
                          <?php } ?>
                      </items>
                  </methods>
              </soap:body>
          </soap:Envelope>';

通过将属性导入变量

的另一种方法
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