http在最佳实践中呼叫

发布于 2025-01-22 23:56:30 字数 1246 浏览 2 评论 0原文

我有两个型号;

export interface Student {
    id: string,
    name: string
}

export interface StudentGoal{
    id: string,
    studentId: string,
    goal: string
}

我让我们的学生服务的学生这样:

getStudents(): Observable<Student[]> {
    return this.http.get<Student[]>("URL_1");
}

我得到了这样的学生目标:

getStudentGoal(studendId: string): Observable<StudentGoal> {
    return this.http.get<StudenGoal>("URL_2" + studentId);
}

问题

最佳实践是什么,最佳实践在我的学生中获得 *ngfor的学生目标。 。成分

.ts

private subs = new SubSink()
public students: Student[] = [];
constructor(
    private studentService: StudentService
)

ngOnInit() {
    this.getStudents();
}

ngOnDestroy() {
    this.subs.unsubscribe()
}

getStudents(){
    this.subs.sink = this.studentService.getStudents().subscribe((res: Student[]) => {
         this.students = res
    })
}

getStudentGoal(studentId: number){
    BEST PRACTICE ?
}

.html

<div *ngFor="let student of students">
    {{getStudentGoal(student.id).goal}}
</div>

I have two models;

export interface Student {
    id: string,
    name: string
}

export interface StudentGoal{
    id: string,
    studentId: string,
    goal: string
}

I get the students in my StudentService like this:

getStudents(): Observable<Student[]> {
    return this.http.get<Student[]>("URL_1");
}

I get a student goal like this:

getStudentGoal(studendId: string): Observable<StudentGoal> {
    return this.http.get<StudenGoal>("URL_2" + studentId);
}

QUESTION

What is the best practice to get student goal inside *ngFor in my student.goal.component

.ts

private subs = new SubSink()
public students: Student[] = [];
constructor(
    private studentService: StudentService
)

ngOnInit() {
    this.getStudents();
}

ngOnDestroy() {
    this.subs.unsubscribe()
}

getStudents(){
    this.subs.sink = this.studentService.getStudents().subscribe((res: Student[]) => {
         this.students = res
    })
}

getStudentGoal(studentId: number){
    BEST PRACTICE ?
}

.html

<div *ngFor="let student of students">
    {{getStudentGoal(student.id).goal}}
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2025-01-29 23:56:30

1.-当我们掌控可观察到时,我们有两个不同的东西事物:“响应”和“订阅”

this.subscription=myObservable(res=>{
     this.response=res;
})

this.subscription是一种订阅,只有在我们想要取消订阅时才util。这不是响应

2.-当我们在.html可观察物中使用时,我们可以

  1. 显示一个我们在subscribe函数中使用的变量

  2. 使用async pipe

      {{{worlds | json}}}
    //或者
    {{myobservable | async}}}
     

不是 {{sisscription}}

3.-当我们掌握ngfor订阅时,有时会读取我们所需的一切。为此,我们使用switchmap和forkjoin,

allData=this.studentService.getStudents().pipe(
   switchMap(students:any[]=>{
        return forkJoin(students.map(student=>
              this.studentService.getStudentGoal(student.id).pipe(
                   //return all the properties of student
                   //and all the properties of getStudentGoal
                   //in an unique object
                   map(x=>({...student,...x})
              )
        )
   })
)

以便我们可以使用

<div *ngFor="let studentData of allData |async">
{{studentData.id}} {{studetData.goal}}
</div>

1.- When we mannage Observables we have two differents things: the "response" and the "subscription"

this.subscription=myObservable(res=>{
     this.response=res;
})

this.subscription is a subscription and only is util when we want unsubscribe. it's NOT the response

2.-When we use in .html observables we can

  1. show a variable that we are using inside subscribe function

  2. use async pipe

    {{response|json}}
    //or
    {{myObservable|async}}
    

but not {{subscription}}

3.-When we mannage a ngFor subscription sometimes is util read all we need. For this we use switchmap and forkJoin

allData=this.studentService.getStudents().pipe(
   switchMap(students:any[]=>{
        return forkJoin(students.map(student=>
              this.studentService.getStudentGoal(student.id).pipe(
                   //return all the properties of student
                   //and all the properties of getStudentGoal
                   //in an unique object
                   map(x=>({...student,...x})
              )
        )
   })
)

So we can use

<div *ngFor="let studentData of allData |async">
{{studentData.id}} {{studetData.goal}}
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文