【问题标题】:In Angular2+ is there any way to extends on inherit const into another const?在 Angular2+ 中,有没有办法将继承 const 扩展到另一个 const?
【发布时间】:2019-03-27 10:00:13
【问题描述】:

我试图继承或扩展 const 到另一个 const

父母.ts

export const Vehicle = {
  'Name': 'Honda',
  'NoOfWheels': '4'
}

Child.ts

import { Vehicle } from './Parent';
export const HondaVehicle = {
    'VehicleName': 'Honda City',
    'Color': 'Red',
    'EngineCC': '2100cc',
}

所以我期待输出

{
      'Name': 'Honda',
      'NoOfWheels': '4',
      'VehicleName': 'Honda City',
      'Color': 'Red',
      'EngineCC': '2100cc',
}

请帮帮我。

【问题讨论】:

标签: angular


【解决方案1】:

正如 trichetriche 所说,您可以使用 spread syntax

import { Vehicle } from './Parent';
export const HondaVehicle = {
   'VehicleName': 'Honda City',
   'Color': 'Red',
   'EngineCC': '2100cc',
    ...Vehicle
}

您也可以使用Object.assign()

import { Vehicle } from './Parent';
export const HondaVehicle = Object.assign({
   'VehicleName': 'Honda City',
   'Color': 'Red',
   'EngineCC': '2100cc',
}, Vehicle)

顺便说一句,这不是 Angular 独有的,它是 javascript 的东西。

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多