Angular 7文件下载

Angular 7文件下载,angular,file,Angular,File,先生,我通过URL下载文件时遇到了一些问题 我的代码片段是这样的 ngOnInit() { this.items = [ {label: '1.txt', value: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg'}, {label: '2.txt', value: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephs

先生,我通过URL下载文件时遇到了一些问题

我的代码片段是这样的

ngOnInit() {
    this.items = [
      {label: '1.txt', value: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg'},
      {label: '2.txt', value: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg'},
      {label: '3.txt', value: 'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg'},
    ];
    this.selectedItem = this.items[0].value;
  }

doDownload() {

}
我将在执行
doDownload()
函数时下载。 如何在angular 7中通过URL下载文件?

参见此

从'@angular/core'导入{Component,OnInit};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
名称='角度';
项目=[];
selectedItem:any;
恩戈尼尼特(){
此项。项目=[
{标签:'1.txt',值:'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' },
{标签:'2.txt',值:'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg' },
{标签:'3.txt',值:'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg' }
];
this.selectedItem=this.items[0]。值;
}
doDownload(值){
var link=document.createElement('a');
link.href=值;
link.download='download.jpg';
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
}
}

请参见