angular6、嵌套参数与httpPost服务
angular 6, nested parameter with http Post service
我有一项服务可以拨打 POST 电话 /albums/{id}/songs
。
下面是相册的api。
POST /albums
private serverUrl = "http://127.0.0.1:3000/api";
private albumApi = "/albums";
private Url = this.serverUrl+this.albumApi;
/** POST: add a new album to the server */
addAlbum(album: Album): Observable<Album> {
return this.http.post<Album>(this.Url, album, httpOptions)
.pipe(
tap((album: Album) => this.log(`added Album w/ id=${album.id}`)),
catchError(this.handleError<Album>('addalbum'))
);
}
但是我需要post专辑的歌曲,所以我必须先获取专辑ID然后执行创建
POST /albums/{id}/songs
如何在获取id的同时创建Post.
谢谢。
/**POST: add a new song to album */
addAlbumSong(albumSong:AlbumSong,id:string):Observable{
const url = ${this.Url}/${id}/songs
;
return this.http.post(url,albumSong,httpOptions)
。管道(
点击((albumSong:AlbumSong)=>this.log(added AlbumSong id=${albumSong.id}
)),
catchError(this.handleError('addAlbumSong'))
)
}
服务
addAlbumSongComp(audio:string,video:string,lyrics:string,name:string,albumId:string):void{
this.route.params.subscribe(params=>{
albumId = params['id'];
if(!name){return}
this.albumService.addAlbumSong({name,audio,video,lyrics,albumId} as AlbumSong,params['id'])
.subscribe(data=>{
this.albumsongs.push(data);
console.log(data);
})
})
}
组件
我有一项服务可以拨打 POST 电话 /albums/{id}/songs
。
下面是相册的api。
POST /albums
private serverUrl = "http://127.0.0.1:3000/api";
private albumApi = "/albums";
private Url = this.serverUrl+this.albumApi;
/** POST: add a new album to the server */
addAlbum(album: Album): Observable<Album> {
return this.http.post<Album>(this.Url, album, httpOptions)
.pipe(
tap((album: Album) => this.log(`added Album w/ id=${album.id}`)),
catchError(this.handleError<Album>('addalbum'))
);
}
但是我需要post专辑的歌曲,所以我必须先获取专辑ID然后执行创建
POST /albums/{id}/songs
如何在获取id的同时创建Post.
谢谢。
/**POST: add a new song to album */
addAlbumSong(albumSong:AlbumSong,id:string):Observable{
const url = ${this.Url}/${id}/songs
;
return this.http.post(url,albumSong,httpOptions)
。管道(
点击((albumSong:AlbumSong)=>this.log(added AlbumSong id=${albumSong.id}
)),
catchError(this.handleError('addAlbumSong'))
)
}
服务
addAlbumSongComp(audio:string,video:string,lyrics:string,name:string,albumId:string):void{
this.route.params.subscribe(params=>{
albumId = params['id'];
if(!name){return}
this.albumService.addAlbumSong({name,audio,video,lyrics,albumId} as AlbumSong,params['id'])
.subscribe(data=>{
this.albumsongs.push(data);
console.log(data);
})
}) }
组件