rxjs

来源:5-6 rxjs 初步了解

这个人

2020-03-31

export interface QuestionCard {
  _id: string,
  questioner: string,
  title: string,
  description: string,
  createdAt: string
}

问题列表:

this.list$ = this.http.get<QuestionCard[]>(`http://questions`)

[

    {

        "_id": "5e7f4cef3a3553366793dc7e",

        "title": "title1?",

        "description": "description1",

        "questioner": "5e71ed676095f90cc049bc0c",

        "createdAt": "2020-03-28T13:11:11.415Z",

       

    },

    {

        "_id": "5e81bd163a3553366793dc8a",

        "title": "title2",

        "description": "description2?",

        "questioner": "5e71ed676095f90cc049bc0c",

        "createdAt": "2020-03-30T09:34:14.978Z",

        

    }

]




我关注的问题列表:

this.my$ = this.http.get<QuestionCard>(`http://users/${this._id}/questions`)

[

{

        "_id": "5e7f4cef3a3553366793dc7e",

        "title": "title1?",

        "description": "description1",

        "questioner": "5e71ed676095f90cc049bc0c",

        "createdAt": "2020-03-28T13:11:11.415Z",

       

    }

]





我没有关注的问题列表:

[

 {

        "_id": "5e81bd163a3553366793dc8a",

        "title": "title2",

        "description": "description2",

        "questioner": "5e71ed676095f90cc049bc0c",

        "createdAt": "2020-03-30T09:34:14.978Z",

        

    }

  ]

怎么得到?

写回答

1回答

接灰的电子产品

2020-03-31

如果你的 api 返回的就是数组,你直接就可以取得啊,前提是你确定是否是 get 方法,且服务端需要的 headers 你都满足,那么 this.http.get<QuestionCard[]>(`http://users/${this._id}/questions`) 就直接可以得到啊

注意 get 后面这个尖括号是反映了http请求返回的json 结构,如果你的 json 返回的就是 QuestionCard 类型的数组,那么这样写之后,Angular 会自动帮你进行 json 到对象直接的转换

1
4
接灰的电子产品
回复
这个人
可以啊,使用 map 就可以啊 pipe(map(all => all.filter(elem => elem.prop === 'xxx'))) ,这里的 all 就是数组,再对数组做一次 filter 不就可以了吗
2020-03-31
共4条回复

Angular 开发拼多多webapp 从基础到项目实战

高仿拼多多WebApp,带你在实战环境中学习Angular

1318 学习 · 451 问题

查看课程

相似问题

rxjs

回答 1

rxjs

回答 1