【发布时间】:2020-11-17 04:20:54
【问题描述】:
我有一个父组件,它使用服务从 api 接收数据。我需要将此数据传递给子组件,因为我不想再次调用 api(严格规则)。我可以访问我的父组件中的数据,但我没有在我的子组件中获取它。我尝试了很多不同的方法。
import { Component, OnInit, ViewChild } from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {SwapiService} from "../../models/swapi.service";
import {Starship} from "../../models/starship";
// import {MatTableDataSource} from '@angular/material/table';
// import {MatPaginator} from '@angular/material/paginator';
@Component({
selector: 'app-starships',
templateUrl: './starships.component.html',
styleUrls: ['./starships.component.css']
})
export class StarshipsComponent implements OnInit {
public starship: Starship[]
url: string;
constructor(private swapi: SwapiService, private route: ActivatedRoute) {
}
ngOnInit(): void {
this.loadStarshipList();
}
loadStarshipList(): void {
this.swapi.getStarshipList().subscribe(
res => {
this.starship = res;
console.log(res);
}
)
}
}
父模板
span *ngIf="starship">
<p *ngFor="let res of starship?.results" >{{res?.name}}
<app-starship *ngFor="let res of starship.results" [data]="res?.results"></app-starship>
<!-- <span [res1]="res1"></span>-->
<button routerLink="/starships/{{res?.url}}">Starship Details</button>
</p>
</span>
子组件
import { Component, Input, OnInit } from '@angular/core';
// import {Starships} from "../../models/starship";
import {SwapiService} from "../../models/swapi.service";
import {ActivatedRoute} from "@angular/router";
import {StarshipsComponent} from "../starships/starships.component";
@Component({
selector: 'app-starship',
templateUrl: './starship.component.html',
styleUrls: ['./starship.component.css']
})
export class StarshipComponent implements OnInit {
// public starship: Starship[]
private url: string;
@Input() data;
constructor(private swapi: SwapiService,
private route: ActivatedRoute,
private activatedRoute: ActivatedRoute,
) {
}
ngOnInit(): void {
this.url = this.route.snapshot.paramMap.get('url');
console.log(this.url);
}
}
子模板
<span *ngIf="data"></span>
<div *ngFor="let res of data">
<h1>{{data?.name}}</h1>
<p>{{data?.url}}</p>
</div>
hi
没有回应。
我正在尝试使用 BehaviorSubject,以便在多个组件之间共享数据。我的问题是,我不知道如何将数据从我的函数传输到一个变量,以便我可以在其他组件中使用它。
这是我的服务文件:
import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {BehaviorSubject, Observable} from "rxjs";
import {map, skipWhile, tap} from 'rxjs/operators';
import {Starship} from "./starship";
@Injectable({
providedIn: 'root'
})
export class SwapiService {
private apiData = new BehaviorSubject<Starship[]>(null)
public currentData = this.apiData.asObservable()
baseURL = 'https://swapi.dev/api/';
constructor(private http: HttpClient) { }
getStarshipList(page?: number): Observable<Starship[]> {
return this.http.get<Starship[]>(`${this.baseURL}starships?format=json${this.getByPage(page)}`)
.pipe(
map(resp => resp['results']),)
;
}
getByPage(page: number): string {
if (page) { return '&page=' + page; } else { return ''; }
}
setData(data) {
this.apiData.next(data)
}
}
如何将 getStarshipList 接收到的数据传输到变量中,然后我可以使用 setData 函数传递它。我知道这似乎是一个愚蠢的问题,但我尝试了许多不同的方法,但都无法正确解决。
@Abhinav 阿加瓦尔
我想出了如何在两个组件中使用它而不在服务中声明变量,但我仍然想知道
【问题讨论】:
-
你能在这里分享响应的完整对象吗?
-
{count: 36, next: "http://swapi.dev/api/starships/?page=2", previous: null, results: Array(10)} count: 36 next: "http://swapi.dev/api/starships/?page=2" previous: null results: Array(10) 0: MGLT: "60" -
cargo_capacity: "3000000" consumables: "1 year" cost_in_credits: "3500000" created: "2014-12-10T14:20:33.369000Z" crew: "30-165" edited: "2014-12-20T21:23:49.867000Z" films: (3) ["http://swapi.dev/api/films/1/", "http://swapi.dev/api/films/3/", "http://swapi.dev/api/films/6/"] hyperdrive_rating: "2.0" length: "150" manufacturer: "Corellian Engineering Corporation" max_atmosphering_speed: "950" model: "CR90 corvette" name: "CR90 corvette" passengers: "600" pilots: [] starship_class: "corvette" url: "http://swapi.dev/api/starships/2/" __proto__: Object@Sivakumar Tadisetti @uiTeam324 -
请不要向 cmets 发布此类内容,请在您的问题下方使用 edit 链接。
标签: angular input angular-decorator