英文:
Sharing API data from parent to service
问题
我正在构建一个网站,显示排名前20的科幻电影。主页组件使用通过服务进行GET请求,以获取包含电影数据的对象数组。这些电影数据使用ngFor*
在HTML模板中进行渲染。
每个电影缩略图都有一个点击处理程序,用于跳转到不同的页面,该页面将显示有关所选电影的更多信息,例如描述、评分等。对于这个第二个页面,我正在使用一个服务进行另一个GET请求,其中查询包含所选电影的ID作为参数。
渲染初始API电影数据的主页组件:
import { Component } from '@angular/core';
import { MovieDataService } from '../services/movie-data.service';
@Component({
selector: 'home-card',
templateUrl: './home-card.component.html',
styleUrls: ['./home-card.component.css']
})
export class HomeCardComponent {
movieData: any = {};
constructor(private movieDataService: MovieDataService) {}
ngOnInit(): void {
this.movieDataService.getData().subscribe((data) => {
this.movieData = data;
// JSON to console
console.warn(data);
})
}
}
主页的服务:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MovieDataService {
constructor(private http: HttpClient) {}
getData() {
return this.http.get('https://api.themoviedb.org/3/discover/movie?api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US&with_genres=878');
}
}
将渲染所选电影数据的单个电影页面:
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { DataService } from '../services/data.service';
@Component({
selector: 'info-page',
templateUrl: './info-page.component.html',
styleUrls: './info-page.component.css'
})
export class InfoPageComponent {
singleMovieData: any = {};
constructor(private DataService: DataService) {}
ngOnInit(): void {
this.DataService.getMovie().subscribe((data) => {
this.singleMovieData = data;
console.warn(data);
})
}
}
单个电影页面的服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
movieId = 505642;
getMovie() {
return this.http.get(`https://api.themoviedb.org/3/movie/${this.movieId}?api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US`)
}
}
目前,我只能在查询中硬编码电影ID,这个方法有效。我还是Angular的新手,正在尝试找到一种动态获取所选电影ID并与服务共享,然后将其作为查询参数之一进行GET请求的方法。
英文:
I am building a website that shows the top 20 rated Sci-fi movies. The homepage component uses a GET request via a service to get an array of objects containing the moviesdata. This movie data is rendered using
ngFor*` in its HTML template.
Each movie thumbnail has a click handler that routes to a different page that will display more information about that selected movie - i.e. description, ratings etc. For this second page, I am using a service that makes another GET request, where the query contains the selected movie's id as a parameter.
Homepage component that renders initial API movie data:
import { Component } from '@angular/core';
import { MovieDataService } from '../services/movie-data.service';
@Component({
selector: 'home-card',
templateUrl: './home-card.component.html',
styleUrls: ['./home-card.component.css']
})
export class HomeCardComponent {
movieData: any = {};
constructor(private movieDataService: MovieDataService) {}
ngOnInit(): void {
this.movieDataService.getData().subscribe((data) => {
this.movieData = data;
// JSON to console
console.warn(data);
})
}
}`
Service for the homepage
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MovieDataService {
constructor(private http: HttpClient) { }
getData() {
return this.http.get('https://api.themoviedb.org/3/discover/movie? api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US&with_genres=878');
}
}`
Single Movie page that will render the selected movie's data
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { DataService } from '../services/data.service';
@Component({
selector: 'info-page',
templateUrl: './info-page.component.html',
styleUrls: ['./info-page.component.css']
})
export class InfoPageComponent {
singleMovieData: any = {} ;
constructor(private DataService: DataService) {}
ngOnInit(): void {
this.DataService.getMovie().subscribe((data) => {
this.singleMovieData = data;
console.warn(data);
})
}
}`
Service for the Single Movie page
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
movieId = 505642;
getMovie() {
return this.http.get(`https://api.themoviedb.org/3/movie/${this.movieId}? api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US`)
}
}
For now, I have only been able to hard code a movieId into the query, which works. I am still very new to angular, and I am trying to find a way to dynamically get the selected movie's id once it has been clicked, share it with the service, and make a GET request with that ID as one of the query's parameters.
答案1
得分: 1
需要将movieId
传递给每个InfoPageComponent
(如果我没记错的话,它是子组件)。
home-card.component.html
<div *ngFor="let movie of movies;">
<info-page [movieId]="movie.movieId"></info-page>
</div>
info-page.component.ts
export class InfoPageComponent {
@Input()
movieId: number;
// 其他代码...
getMovie() {
this.dataService.getMovie(this.movieId).subscribe(...);
}
}
这是你要的翻译部分。
英文:
You need to pass movieId
to your each InfoPageComponent
(which is child component, if I am not wrong)
home-card.component.html
<div *ngFor="let movie of movies;">
<info-page [movieId]="movie.movieId"></info-page>
</div>
info-page.component.ts
export class InfoPageComponent {
@input()
movieId: number;
....
getMovie() {
this.dataService.getMovie(this.movieId).subscribe(...);
}
}
答案2
得分: 0
以下是翻译好的部分:
info-page.service.ts
public getMovie(movieId: number): IMovie {
return this.http.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US`)
}
英文:
Along with paranaaan's answer,
info-page.service.ts
public getMovie(movieId: number): IMovie {
return this.http.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=9279f87c4b7f1cb1474b6d7cd6958a6d&language=en-US`)
}
答案3
得分: 0
- 定义路由为
{ path: 'movieInfo/:id', component: InfoPageComponent }
- 导航至路由
<a [routerLink]="['/movieInfo', movie.id]">{{movie.name}} </a>
- 在 InfoPageComponent 中获取路由参数
this.id = this._Activatedroute.snapshot.paramMap.get("id");
英文:
- Define route as
{ path: 'movieInfo/:id', component: InfoPageComponent }
- Navigate to the route
<a [routerLink]="['/movieInfo', movie.id]">{{movie.name}} </a>
- get route parameter in InfoPageComponent
this.id=this._Activatedroute.snapshot.paramMap.get("id");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论