【问题标题】:Angular tour of heroes, How is the ID added in the addHero method?角游英雄,addHero方法中如何添加ID?
【发布时间】:2018-03-12 13:27:08
【问题描述】:

在Agular的英雄之旅官方教程(https://angular.io/tutorial/toh-pt6)

调用 addHero 方法时,只传递了英雄的 name 属性,但不知何故创建了一个具有 id 属性的新英雄对象。 我可能遗漏了一些东西,但是 id 属性是从哪里来的?

【问题讨论】:

  • 卡在同一个地方...赞成。

标签: javascript angular object observable


【解决方案1】:

这个 Angular 应用程序使用 angular-in-memory-web-api 包来拦截所有 http 请求并隐藏一些功能:

protected post(...): ResponseOptions {
  const item = this.clone(this.getJsonBody(req));

  if (item.id == undefined) {
    try {
      item.id = id || this.genId(collection, collectionName);
                      ^^^^^^^^^^^^^
                     generate id

https://github.com/angular/in-memory-web-api/blob/25c6a217986695de619feed35851332636769a05/src/in-mem/backend.service.ts#L590-L592

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2019-10-09
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多