在常规JavaScript中,我们可以使用字符串为名称向window
全局对象添加属性,如下所示:
const str = "myVar";
window[str] = "Value";
console.log(myVar);
但是在Angular 2/4和Typescript中是否有一种方式可以完成类似的工作呢?我们可以使用this.myVar
将常规变量存储在组件中,但是是否有一种方法可以使用字符串来创建相同的变量名称?例如:
const str = "myVar";
this[str] = "Value";
// the result should be the similar as this.myVar = "Value";
window
对象添加属性。 - Henryconsole.log
。 - P.S.