将变量声明为对象值时,TSlint错误“对象文字中的预期属性简写…”

我有一个这样的对象:

   {
      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因为属性名称与您的常量相同。

所以你的新对象应该是这样的:

{
  element: 'tool-app',
  file: '/tool-app.js',
  icon,
  name: 'Planning view',
  id: 'planning-view'
}

这称为“速记”:)


以上是将变量声明为对象值时,TSlint错误“对象文字中的预期属性简写…”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>