【问题标题】:Can't find image on localhost, 404 error in angular在本地主机上找不到图像,角度出现 404 错误
【发布时间】:2020-09-16 20:54:30
【问题描述】:

我有一个 Angular 8 应用程序。

我有这样的服务:


@Injectable({
  providedIn: 'root'
})
export class GetProfileImageUrl {
  profileImagefile: File;

  constructor(private sanitizer: DomSanitizer) {}

  profileImageUrlDefault() {
    return this.profileImagefile === null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
  }
}

我有一个组件,我在其中调用这个函数,如下所示:

  get profileImageUrl() {
    return this.getImageUrl.profileImageUrlDefault;
  }

在构造函数中我有这个:

private getImageUrl: GetProfileImageUrl

模板看起来像这样:

<img [src]="profileImageUrl" width="147px" />

但我收到此错误:

profileImageUrlDefault()%20%7B%20%20%20%20%20%20%20%20return%20this.profileImagefile%20===%20null%20%20%20%20%20%20%20%20%20%20%20%20:1 GET http://localhost:4200/profileImageUrlDefault()%20%7B%20%20%20%20%20%20%20%20return%20this.profileImagefile%20===%20null%20%20%20%20%20%20%20%20%20%20%20%20?%20%27/assets/placeholder.jpg%27%20%20%20%20%20%20%20%20%20%20%20%20:%20this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));%20%20%20%20} 404 (Not Found)
Image (async)

所以我的问题是。我必须改变什么?

谢谢

对不起,这是我打的:

如果我这样做:

 get profileImageUrl() {
    return this.getImageUrl.profileImageUrlDefault();
  }

我收到此错误:

core.js:5871 ERROR TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
    at GetProfileImageUrl.profileImageUrlDefault (get-profile-image-url.ts:15)
    at ProfileInformationComponent.get profileImageUrl [as profileImageUrl] (profile-information.component.ts:86)
    at ProfileInformationComponent_Template (profile-information.component.html:3)
    at executeTemplate (core.js:11926)
    at refreshView (core.js:11773)
    at refreshComponent (core.js:13213)
    at refreshChildComponents (core.js:11504)
    at refreshView (core.js:11825)
    at refreshComponent (core.js:13213)
    at refreshChildComponents (core.js:11504)

【问题讨论】:

  • 您没有调用该函数。 return this.getImageUrl.profileImageUrlDefault 应该是 return this.getImageUrl.profileImageUrlDefault()
  • 是的,对不起我的错误,我更新了帖子
  • @NiceTobeBottleInfinity,请在下面尝试我的答案并告诉我。

标签: javascript angular typescript


【解决方案1】:

在服务文件中,需要进行一项更改。三重方程 (===) 应替换为双 (==)。

@Injectable({
providedIn: 'root'
})
export class GetProfileImageUrl {
  profileImagefile: File;

  constructor(private sanitizer: DomSanitizer) {}

  profileImageUrlDefault() {
    return this.profileImagefile == null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
  }
}

应该在打字稿文件中进行另一项更改。这里应该调用服务文件中包含的 profileImageUrlDefault 函数,如下所示。

get profileImageUrl() {
  return this.getImageUrl.profileImageUrlDefault();
}

link 让您可以访问实时的 stackblitz 环境,在进行上述调整后,解决方案可以正常工作。

希望这个答案可能会有所帮助。

【讨论】:

  • 感谢您的回复。是的,我没有收到任何错误。但是现在的问题是我总是得到默认的('/assets/placeholder.jpg')图像而不是选定的图像。
  • 我根据上面提供的详细信息提供了解决方案。似乎只发布了您的部分逻辑......在这里我找不到任何选择事件逻辑或与之相关的任何内容。尝试提供包含所有相关代码的完整逻辑或实时 stavkblitz 环境链接..
  • 好吧,我必须在服务中将 profileImagefile: File 作为参数传递给它。但我接受你的回答。所以谢谢你让我走上正确的道路。
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 2018-10-06
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2014-02-10
  • 2017-12-09
相关资源
最近更新 更多