【发布时间】:2018-12-05 09:31:07
【问题描述】:
我在 localhost:3000 上运行了一个 nodejs 应用程序,我使用 multer 上传了图像,所以它们位于 ./uploads/ 文件夹中。 在 locahost:4200 上运行的 Angular 应用程序中,我想检索这些图像。
在我的 for 循环中:(projectImages 是每个项目对象的图像数组)
<div *ngFor="let i of project.projectImages">
<img [src]=i.path alt="" >
</div>
问题是路径显示为:localhost:4200/uploads/image.png 而不是 localhost:3000/uploads/image.png
更新: 通过向组件添加一个变量来解决这个问题,我现在得到:
WARNING: sanitizing unsafe URL value
任何帮助将不胜感激!
更新2: 这是我的组件:
import { Component, OnInit } from '@angular/core';
import { ProjectService } from '../services/project.service';
import { Router, ActivatedRoute } from '@angular/router';
import {Location} from '@angular/common';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
selector: 'app-project-details',
templateUrl: './project-details.component.html',
styleUrls: ['./project-details.component.css']
})
export class ProjectDetailsComponent implements OnInit {
project:any;
apiUrl :string = "localhost:3000";
project_id:string;
constructor(
private router:Router,
private activatedRoute: ActivatedRoute,
private projectService:ProjectService,
private _location: Location,
private sanitization: DomSanitizer
) {
this.activatedRoute.params
.subscribe( params => {
console.log(params.project_id)
this.project_id = params.project_id;
this.getProjectByID(params.project_id);
})
}
ngOnInit() {
}
getProjectByID(project_id:string){
this.projectService.getProjectById(project_id).subscribe((data:any) => {
this.project = data.project;
this.project.projectImages.map(image => {
image.path = this.sanitization.bypassSecurityTrustUrl(`${this.apiUrl}/${image.path}`.replace(/\\/g,"/"));
console.log(image);
return image;
});
console.log(this.project.projectImages);
} , err => {
console.log(err);
});
}
}
注意:project.projectImages 是一个包含图像的数组,如下所示:https://ibb.co/jEgszo
【问题讨论】:
-
您使用的是相对路径。如果您想使用 3000 而不是 4200,请给出 [src] 的完整路径。
[src]='localhost:3000\'+i.path或者您可以将地址localhost:3000存储在 ts 文件中的变量中并在 html 中使用该变量 -
我已经试过了,它说“报价不支持评估!”
-
不,这不是错字,i.path 来自 for 循环,需要与 "localhost:3000\" 连接 .. 我在这上面花了太多时间
-
所以我声明了一个变量并将 localhost:3000 设置为它,现在我收到 WARNING: sanitizing unsafe URL value
-
使用
[src]='server+i.path'。在.ts中设置server = "localhost:3000/"