[英]TSlint error “Expected property shorthand in object literal…” when declaring a variable to a object value
我有一个像这样的 object:
{
element: 'tool-app',
file: '/tool-app.js',
icon: 'csr-icon',
name: 'Planning view',
id: 'planning-view'
}
我想将 icon 的值存储在一个变量中并使用它。 首先我定义一个常量:
const icon = 'csr-icon';
然后我尝试对 object 使用上面的图标:
{
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 相同。
所以你的新 object 应该是这样的:
{
element: 'tool-app',
file: '/tool-app.js',
icon,
name: 'Planning view',
id: 'planning-view'
}
这被称为“速记”:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.