简体   繁体   English

Angular 2 - 合并(扩展)对象

[英]Angular 2 - Merging (extending) objects

How to merge 2 objects with Angular 2?如何将 2 个对象与 Angular 2 合并?

In AngularJS 1 we have the "merge" and "extend" functions: https://docs.angularjs.org/api/ng/function/angular.merge https://docs.angularjs.org/api/ng/function/angular.extend在 AngularJS 1 中,我们有“合并”和“扩展”功能: https : //docs.angularjs.org/api/ng/function/angular.merge https://docs.angularjs.org/api/ng/function/ angular.extend

But apparently nothing in Angular 2!但显然 Angular 2 中什么都没有!

Do you have an idea?你有什么想法吗?

Thanks!谢谢!

Angular2 doesn't provide anything like this. Angular2 没有提供这样的东西。

You can use for example Object.assign()您可以使用例如Object.assign()

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

The way to do it in Angular is with javascript's Object Spread syntax or Object.assign() :在 Angular 中这样做的方法是使用 javascript 的Object Spread语法或Object.assign()

const obj = {...object1, ...object2}; // or
const obj = Object.assign({}, object1, object2);

The above options are not supported by IE natively. IE本身不支持上述选项。
So let's see what happens in each scenario...那么让我们看看在每个场景中会发生什么......

1. Angular CLI 1. Angular CLI

Version 8 and higher版本 8 及更高版本

In Angular CLI version 8 and higher, you don't need to take any additional actions.在 Angular CLI 版本 8 及更高版本中,您无需执行任何其他操作。 Applications are built using differential loading.应用程序是使用差分加载构建的。 This basically means that Angular will load a different javascript bundle for older browsers, which includes all the necessary polyfills.这基本上意味着 Angular 将为旧浏览器加载不同的 javascript 包,其中包括所有必要的 polyfill。 Read more about browser support .阅读有关浏览器支持的更多信息

Older versions旧版本

If you are using older versions of the Angular CLI with a Typescript version less than 2.1 (quite old), then open /src/polyfills.ts and uncomment the following line:如果您使用的是 Typescript 版本低于 2.1(相当旧)的旧版本 Angular CLI,请打开/src/polyfills.ts并取消注释以下行:

// import 'core-js/es6/object';

This will enable the Object polyfill which provides cross browser support for Object.assign() .这将启用为Object.assign()提供跨浏览器支持的 Object polyfill。

2. Typescript 2.打字稿

If you did not use Angular CLI, but you are using Typescript version 2.1 or greater, then you don't have to take any additional actions.如果您没有使用 Angular CLI,但您使用的是 Typescript 2.1 或更高版本,那么您无需采取任何其他操作。 Otherwise...否则...

3. Plain Javascript 3. 纯 Javascript

If you don't use Angular CLI or Typescript, then you can just import a polyfill into your project.如果您不使用 Angular CLI 或 Typescript,那么您只需将一个 polyfill 导入您的项目即可。 There are plenty out there and here are a few ones:那里有很多,这里有一些:


Babel is a good option. Babel是一个不错的选择。 You can use any of theObject.assign() and Object Spread polyfill plugins.您可以使用任何Object.assign()Object Spread polyfill 插件。


Another options is core-js npm package that Angular CLI uses for its polyfills.另一个选项是 Angular CLI 用于其 polyfill 的core-js npm 包


A simple polyfill from Typescript's conversion into javascript:从 Typescript 转换为 javascript 的简单polyfill:

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;
};

And lastly, the MDN polyfill .最后, MDN polyfill

You can use underscore/lodash library.您可以使用下划线/lodash 库。 https://lodash.com/docs#merge https://lodash.com/docs#merge

Just include it in your angular2/4 stack.只需将它包含在您的 angular2/4 堆栈中。
This question explains how: Importing lodash into angular2 + typescript application这个问题解释了如何: 将 lodash 导入 angular2 + typescript 应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM