Javascript 错误TS2539:无法分配给';c';因为它不是一个变量
我有2.ts文件 C.ts: 答: 编译A.ts时,出现一个错误:Javascript 错误TS2539:无法分配给';c';因为它不是一个变量,javascript,typescript,Javascript,Typescript,我有2.ts文件 C.ts: 答: 编译A.ts时,出现一个错误: error TS2539: Cannot assign to 'c' because it is not a variable. 如何修复它?无法分配c变量,因为您使用let export let c: any = 10; 你必须像这样付出 export c: any = 10; 看,这里有点混乱。Axel Rauschmayer博士把它钉在 CommonJS模块导出值。ES6模块导出绑定-实时 与值的连接 所以你有两个
error TS2539: Cannot assign to 'c' because it is not a variable.
如何修复它?无法分配c变量,因为您使用let
export let c: any = 10;
你必须像这样付出
export c: any = 10;
看,这里有点混乱。Axel Rauschmayer博士把它钉在 CommonJS模块导出值。ES6模块导出绑定-实时 与值的连接
所以你有两个选择:
- 调整编译器选项,使您的模块被视为CommonJS模块
- 将导入的值视为绑定(别名),而不是真正的标识符
export class GlobalVars {
public static c: any = 10;
}
从任何其他文件导入后
GlobalVars.c = 100;
将对象用作名称空间:
导出let状态={
c:10为编号;
}
我怀疑c
是一个let
,所以它的作用域是导入的文件?希望这有帮助相关:谢谢你的回答!如果我的理解是正确的,当它们是绑定时,c=100
应该“将标识符c
重新绑定到对象100
”,但是c
不是一个实际的标识符。这似乎是一个比公认答案更优雅的解决方案
//------ lib.js ------
export let mutableValue = 3;
export function incMutableValue() {
mutableValue++;
}
//------ main1.js ------
import { mutableValue, incMutableValue } from './lib';
// The imported value is live
console.log(mutableValue); // 3
incMutableValue();
console.log(mutableValue); // 4
// The imported value can’t be changed
mutableValue++; // TypeError
export class GlobalVars {
public static c: any = 10;
}
GlobalVars.c = 100;