2016-04-03 20 views

risposta

24

Angular2 non fornisce nulla di simile.

È possibile utilizzare ad esempio Object.assign()

Object.assign(target, source_1, ..., source_n) 
+6

Attenzione con IE. Secondo MDN non è supportato. – lukeatdesignworks

+1

Il browser Android non supporta questo: http://kangax.github.io/compat-table/es6/#test-Object_static_methods_Object.assign – Chris

+4

Se si utilizza la CLI Angolare, trovare 'poyfills.ts' e decommenta il codice che supporta IE9-11. – shammelburg

13

Secondo MDN, Object.assign() non è ancora supportato da IE e Android. Se si installa una versione tipografico di 2,1 o superiore, però, è possibile utilizzare Object Spread invece:

let obj = { x: 1, y: "string" }; 
var newObj = {...obj, z: 3, y: 4}; // { x: number, y: number, z: number } 

Se non si desidera utilizzare tipografico, quindi ecco una simple polyfill dalla conversione di tipografico del sopra in JavaScript :

Object.assign = Object.assign || function(t) { 
    for (var s, i = 1, n = arguments.length; i < n; i++) { 
     s = arguments[i]; 
     for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) 
      t[p] = s[p]; 
    } 
    return t; 
}; 

var obj = { x: 1, y: "string" }; 
var newObj = Object.assign({}, obj, { z: 3, y: 4 }); 

View on Typescript Playground