本文介绍了使用TypeScript和Object.assign给我一个错误“属性'assign'在类型'ObjectConstructor'上不存在'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在再次写我的问题,因为早些时候它没有多大意义,而且我不太清楚。

I am writing my question again because earlier it made little sense and I wasn't very clear.

我从API接收的数据看起来像这样:

I am receiving data from API that looks something like this:

{"photos":[{"id":1,"title":"photo_1_title"}]}

所以,在我的代码中,我有一张的照片变量,以及一个名为 getPhotos()的方法

So, in my code, I have a photos variable, and a method called getPhotos()

我正在使用无限滚动,所以当我到达页面底部时,我再次拨打 getPhotos()

I am using infinite scroll so when I reach the bottom of the page, I call getPhotos() again.

photos: any;

getPhotos() {
  this.photoService.getPhotos()
    .subscribe(
      photos => this.photos = photos
      // here, instead of doing this, I want to add the array of photos I get back to this.photos using Object.assign however it is giving me the said error
    )
}

因此,如果我下次打电话,我会回来 {photos:[{id :2,title:photo_2_title}]} ,然后我试图将 this.photos 设为

So if the next time I call it, I get back {"photos":[{"id":2,"title":"photo_2_title"}]}, then I am trying to set this.photos to be

{"photos":[{"id":1,"title":"photo_1_title"}, {"id":2,"title":"photo_2_title"}]}

有人可以帮我解释为什么

jsfiddle.net/ca46hLw9不起作用?我认为assign应该合并一个对象的内容吗?

can someone help me with why
jsfiddle.net/ca46hLw9 doesn't work? I thought assign is supposed to merge contents of an object together right?

推荐答案

Object.assign 是一个ECMAScript2015功能,不存在于ECMAScript5及更低版本。

Object.assign is an ECMAScript2015 feature and does not exist in ECMAScript5 and lower.

您最有可能的目标是编译ECMAScript5的Typescript,因此Object接口没有 assign 已定义。

You're most likely targeting to compile your Typescript for ECMAScript5 and therefor the Object interface does not have assign defined.

您可以通过更改TS编译器配置来定位ECMAScript2015

You can either target ECMAScript2015 by changing your TS compiler configuration with

target:'es6'

或者您可以使用 assign

declare interface ObjectConstructor {
    assign(...objects: Object[]): Object;
}

(您可以在任何地方添加此声明,重新声明接口扩展它而不是覆盖它)

(you can add this declaration anywhere, redeclaring an interface extends it instead of overwriting it)

或者你可以强制对象任何并忽略其输入:

Or you can coerce Object to any and ignore its typing:

(< any> Object).assign(this.photos,photos)

无论您选择哪种方式,请记住,如果您希望在旧版浏览器上运行,则需要添加polyfill。大多数现代浏览器都非常接近于实现ES2015功能集,但它们并非100%存在,这意味着如果您依赖ES2015功能,代码的某些部分仍然会失败。

Whichever you choose, keep in mind that if you want this to run on older browsers you need to add a polyfill. Most modern browsers are fairly close to implementing the ES2015 feature set, but they're not 100% there, meaning that some parts of your code will still fail if you rely on ES2015 functionality.

在目标ES5或更早版本时,Typescript不会填充 Object.assign ,这就是您收到此错误的原因。另一方面,Babel确实有插件形式的polyfill,这意味着你需要将Babel transpilation合并到你的构建管道中,参见:

Typescript will not polyfill Object.assign when targetting ES5 or older, that is why you are getting this error. On the other hand, Babel does have polyfills in the form of plugins, which would mean you need to incorporate Babel transpilation into your build pipeline, see: https://babeljs.io/docs/plugins/transform-object-assign/

作为最后的选择,你可以考虑使用一个库作为Lodash或jQuery,它们有自己的 Object.assign 的实现(称为 extend

As a final option you can consider using a library as Lodash or jQuery, which have their own implementation of Object.assign (called extend)

这篇关于使用TypeScript和Object.assign给我一个错误“属性'assign'在类型'ObjectConstructor'上不存在'&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:31