在Angular2 +中,是否有任何方法可以将继承const扩展为另一个const?

时间:2019-03-27 10:00:14

标签: angular

我试图继承const或将其扩展为另一个const

Parent.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',
}

请帮助我。

1 个答案:

答案 0 :(得分:2)

正如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东西。