我有一个这样的对象:
{
element: 'tool-app',
file: '/tool-app.js',
icon: 'csr-icon',
name: 'Planning view',
id: 'planning-view'
}
我想将 icon 的值存储在一个变量中并使用它。首先我定义一个常量:const icon = 'csr-icon';
然后我尝试对对象使用上面的图标:{
element: 'tool-app',
file: '/tool-app.js',
icon: icon, ///change here
name: 'Planning view',
id: 'planning-view'
}
它应该是相等的,但是 Tslint 返回一个错误:Expected property shorthand in object literal ('{icon}'). (object-literal-shorthand)tslint(1)
怎么来的?
最佳答案
这个错误告诉你,而不是 icon: icon,
, 你只能写 icon
因为属性名称与您的 const 相同。
所以你的新对象应该是这样的:
{
element: 'tool-app',
file: '/tool-app.js',
icon,
name: 'Planning view',
id: 'planning-view'
}
这被称为“速记”:)
关于javascript - 将变量声明为对象值时出现 TSlint 错误 "Expected property shorthand in object literal...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66743443/