在Angular 8中,可以使用Angular的数据绑定语法来设置图片路径。
[src]
来绑定图片路径。例如:
export class MyComponent {
imagePath: string = 'assets/images/my-image.jpg';
}
请注意,这里假设图片位于assets/images
目录下。
HttpClient
来获取数据,并将获取的图片路径赋值给imagePath
变量。import { HttpClient } from '@angular/common/http';
export class MyComponent {
imagePath: string;
constructor(private http: HttpClient) {
this.http.get('api/getImagePath').subscribe((response: any) => {
this.imagePath = response.imagePath;
});
}
}
这里假设通过API获取到的图片路径是response.imagePath
。
这样,当组件初始化时,图片路径就会被赋值给imagePath
变量,然后在HTML模板中使用数据绑定语法来动态设置图片路径。