如何使用角度检索路径存储在数据库中的图像



我想显示存储在数据库中的图像。数据库存储图像的路径。我想使用角度检索图像。我该怎么做?

您需要创建一个服务来调用API来获取图像URL(我假设图像存储在JSON文件中(

   @Injectable()
    export class ConfigService {
      constructor(private http: HttpClient) { }
      getData() {
        return this.http.get('/assets/config.json');
      }
    }

创建组件 ts 文件以调用服务并返回图像 URL

export class AppComponent {
  name = 'Test display image';
  thumbnail: any;
  constructor(private imageService: ConfigService, private sanitizer: DomSanitizer) { }
  ngOnInit() {
     this.imageService.getData()
      .subscribe((baseImage : any) => {
        let objectURL = baseImage.image;
         this.thumbnail = objectURL;
      });
  }
}

更新 HTML 文件以显示图像

<img width="100%" id="myimage" [src]='thumbnail' />

演示:https://stackblitz.com/edit/display-imagepath-from-api

最新更新