无法第二次调用 angular 服务
Unable to call angular service the second time
我正在尝试通过单击 angular 中的按钮来调用服务函数。如果我以正确的方式做,我不会。第一次我能够调用该服务并第二次检索值时,当我单击该按钮时,它会抛出以下错误。
[这是错误]
core.js:4197 ERROR TypeError: this._posterService.getPosterUrl is not a function
at HomeComponent.downloadFile (home.component.ts:63)
at HomeComponent_div_31_Template_a_click_9_listener (home.component.html:56)
at executeListenerWithErrorHandling (core.js:14316)
at wrapListenerIn_markDirtyAndPreventDefault (core.js:14351)
at HTMLAnchorElement.<anonymous> (platform-browser.js:582)
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27425)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)
这是我的代码。
<a (click)="downloadFile(product._id)" class="genric-btn primary" style="color: aliceblue;">Download</a>
downloadFile(value:any):void{
this.subs = this._posterService.getPosterUrl(value).subscribe(res => {
this._posterService = res;
console.log(res)
this.DownloadUrl = "assets/img/test.pdf"
let link = document.createElement("a");
link.download = "filename";
link.href = this.DownloadUrl;
link.click();
});
}
我在这里订阅服务。当我单击按钮时,我调用了函数并将 ID 传递给函数。
public getPosterUrl=(posterId:string):Observable<any> =>{
return this._http.get('http://localhost:3000/api/posters/'+posterId);
}
我能够获得对服务的第一个请求。当我第二次尝试单击按钮时。抛出错误。
您使用的语法不正确,public getPosterUrl
后不应有 = 符号
应该是
public getPosterUrl(posterId:string):Observable<any> => {
return this._http.get('http://localhost:3000/api/posters/'+posterId);
}
使用这个:
this._posterService = res;
您正在丢失由 angular 注入的服务实例。因此,您只能 运行 一次服务功能,只需删除该行或尝试解释您尝试用它实现的替代解决方案。
我正在尝试通过单击 angular 中的按钮来调用服务函数。如果我以正确的方式做,我不会。第一次我能够调用该服务并第二次检索值时,当我单击该按钮时,它会抛出以下错误。 [这是错误]
core.js:4197 ERROR TypeError: this._posterService.getPosterUrl is not a function
at HomeComponent.downloadFile (home.component.ts:63)
at HomeComponent_div_31_Template_a_click_9_listener (home.component.html:56)
at executeListenerWithErrorHandling (core.js:14316)
at wrapListenerIn_markDirtyAndPreventDefault (core.js:14351)
at HTMLAnchorElement.<anonymous> (platform-browser.js:582)
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27425)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)
这是我的代码。
<a (click)="downloadFile(product._id)" class="genric-btn primary" style="color: aliceblue;">Download</a>
downloadFile(value:any):void{
this.subs = this._posterService.getPosterUrl(value).subscribe(res => {
this._posterService = res;
console.log(res)
this.DownloadUrl = "assets/img/test.pdf"
let link = document.createElement("a");
link.download = "filename";
link.href = this.DownloadUrl;
link.click();
});
}
我在这里订阅服务。当我单击按钮时,我调用了函数并将 ID 传递给函数。
public getPosterUrl=(posterId:string):Observable<any> =>{
return this._http.get('http://localhost:3000/api/posters/'+posterId);
}
我能够获得对服务的第一个请求。当我第二次尝试单击按钮时。抛出错误。
您使用的语法不正确,public getPosterUrl
后不应有 = 符号
应该是
public getPosterUrl(posterId:string):Observable<any> => {
return this._http.get('http://localhost:3000/api/posters/'+posterId);
}
使用这个:
this._posterService = res;
您正在丢失由 angular 注入的服务实例。因此,您只能 运行 一次服务功能,只需删除该行或尝试解释您尝试用它实现的替代解决方案。