如何使用 http Angular 访问对象内部的数组?
How to access array inside the object with http Angular?
我有以下结构 db.json
{
"teams": [
...
],
"matches": [
{ // matchday
"id": "4b6023d0-8657-11ea-ab9d-57972c99f38c",
"matches": [
{
...
"id": "4b604ae0-8657-11ea-ab9d-57972c99f38c"
},
...
]
},
{...},
{...},
]
}
如何更新 ID 匹配项?
我试过了
const MATCHES_API = '/api/matches';
editMatch(match: Match, matchday: Matchday): Observable<Match> {
return this.http
.put(`${MATCHES_API}/${matchday.id}/matches/${match.id}`, match)
.map((response: Response) => response.json());
}
另外,当我输入 /api/matches/${matchday.id}/matches/${match.id}
时,它以某种方式将我返回到 /api/matches/
。
我怎样才能到达这里的比赛?
更新:组件的class,我尝试实现它
import { Component, OnInit } from '@angular/core';
import { PremierLeagueService } from '../../premier-league.service';
import { Matchday } from '../../models/matchday.interface';
import { Match } from '../../models/match.interface';
@Component({
selector: 'league-matches',
styleUrls: ['league-matches.component.scss'],
template: `
<div
class="matchday"
*ngFor="let matchday of matches; let i = index">
<h2>Matchday {{ i + 1 }}</h2>
<match-item
*ngFor="let match of matchday.matches; let i = index; let c = count"
[match]="match"
[matchday]="matchday"
[teamIndex]="i"
[teamAmount]="c"
(submitScore)="submitScore($event)"
(getMatchday)="getMatchday($event)">
</match-item>
</div>
`
})
export class LeagueMatchesComponent implements OnInit{
constructor(private premierLeagueService: PremierLeagueService) {}
matches: Matchday[];
currentMatchday: Matchday;
ngOnInit() {
this.premierLeagueService
.getAllMatchdays()
.subscribe((data: Matchday[]) => this.matches = data);
}
submitScore(match: Match) {
this.premierLeagueService
.editMatch(match)
.subscribe((data: Match) => {
this.matches.forEach((matchdayToCheck: Matchday) => {
matchdayToCheck.matches.forEach((matchToCheck: Match) => {
if (matchdayToCheck.id === match.id) {
matchToCheck = Object.assign({}, matchToCheck, match);
}
})
})
})
}
getMatchday(matchday: Matchday) {
console.log(matchday);
this.currentMatchday = matchday;
}
}
这是我在尝试编辑比赛(添加比赛分数)时得到的结果
我在第一个 matchday
中尝试访问 matches array
http://localhost:4000/api/matches/4b6023d0-8657-11ea-ab9d-57972c99f38c
http://localhost:4000/api/matches/${matchday.id}
通过 ID 访问了第一个比赛日
如您所见,matchday
有 ID
和 matches array
正在尝试访问匹配项
http://localhost:4000/api/matches/4b6023d0-8657-11ea-ab9d-57972c99f38c/4b604ae0-8657-11ea-ab9d-57972c99f38c
http://localhost:4000/api/matches/${matchday.id}/${match.id}
哪个returns我这个
更新 2.0:
这是我的本地数据库通过 webpack 连接的方式
devServer: {
contentBase: cwd,
compress: true,
inline: true,
hot: true,
port: 4000,
publicPath: '/build/',
quiet: true,
historyApiFallback: true,
setup: function (app) {
app.use('/api', jsonServer.router('db.json'));
},
stats: {
chunks: false,
chunkModules: false
}
},
如果您使用的是简单的 json 服务器,例如 this
它应该可以工作 url
`${MATCHES_API}/{match.id}`
const MATCHES_API = '/api/matches';
editMatch(match: Match, matchday: Matchday): Observable<Match> {
return this.http
.put(`${MATCHES_API}/{match.id}`, match)
.map((response: Response) => response.json());
}
我有以下结构 db.json
{
"teams": [
...
],
"matches": [
{ // matchday
"id": "4b6023d0-8657-11ea-ab9d-57972c99f38c",
"matches": [
{
...
"id": "4b604ae0-8657-11ea-ab9d-57972c99f38c"
},
...
]
},
{...},
{...},
]
}
如何更新 ID 匹配项?
我试过了
const MATCHES_API = '/api/matches';
editMatch(match: Match, matchday: Matchday): Observable<Match> {
return this.http
.put(`${MATCHES_API}/${matchday.id}/matches/${match.id}`, match)
.map((response: Response) => response.json());
}
另外,当我输入 /api/matches/${matchday.id}/matches/${match.id}
时,它以某种方式将我返回到 /api/matches/
。
我怎样才能到达这里的比赛?
更新:组件的class,我尝试实现它
import { Component, OnInit } from '@angular/core';
import { PremierLeagueService } from '../../premier-league.service';
import { Matchday } from '../../models/matchday.interface';
import { Match } from '../../models/match.interface';
@Component({
selector: 'league-matches',
styleUrls: ['league-matches.component.scss'],
template: `
<div
class="matchday"
*ngFor="let matchday of matches; let i = index">
<h2>Matchday {{ i + 1 }}</h2>
<match-item
*ngFor="let match of matchday.matches; let i = index; let c = count"
[match]="match"
[matchday]="matchday"
[teamIndex]="i"
[teamAmount]="c"
(submitScore)="submitScore($event)"
(getMatchday)="getMatchday($event)">
</match-item>
</div>
`
})
export class LeagueMatchesComponent implements OnInit{
constructor(private premierLeagueService: PremierLeagueService) {}
matches: Matchday[];
currentMatchday: Matchday;
ngOnInit() {
this.premierLeagueService
.getAllMatchdays()
.subscribe((data: Matchday[]) => this.matches = data);
}
submitScore(match: Match) {
this.premierLeagueService
.editMatch(match)
.subscribe((data: Match) => {
this.matches.forEach((matchdayToCheck: Matchday) => {
matchdayToCheck.matches.forEach((matchToCheck: Match) => {
if (matchdayToCheck.id === match.id) {
matchToCheck = Object.assign({}, matchToCheck, match);
}
})
})
})
}
getMatchday(matchday: Matchday) {
console.log(matchday);
this.currentMatchday = matchday;
}
}
这是我在尝试编辑比赛(添加比赛分数)时得到的结果
我在第一个 matchday
matches array
http://localhost:4000/api/matches/4b6023d0-8657-11ea-ab9d-57972c99f38c
http://localhost:4000/api/matches/${matchday.id}
通过 ID 访问了第一个比赛日
matchday
有 ID
和 matches array
正在尝试访问匹配项
http://localhost:4000/api/matches/4b6023d0-8657-11ea-ab9d-57972c99f38c/4b604ae0-8657-11ea-ab9d-57972c99f38c
http://localhost:4000/api/matches/${matchday.id}/${match.id}
哪个returns我这个
更新 2.0:
这是我的本地数据库通过 webpack 连接的方式
devServer: {
contentBase: cwd,
compress: true,
inline: true,
hot: true,
port: 4000,
publicPath: '/build/',
quiet: true,
historyApiFallback: true,
setup: function (app) {
app.use('/api', jsonServer.router('db.json'));
},
stats: {
chunks: false,
chunkModules: false
}
},
如果您使用的是简单的 json 服务器,例如 this
它应该可以工作 url
`${MATCHES_API}/{match.id}`
const MATCHES_API = '/api/matches';
editMatch(match: Match, matchday: Matchday): Observable<Match> {
return this.http
.put(`${MATCHES_API}/{match.id}`, match)
.map((response: Response) => response.json());
}