angular - 类型“SafeResourceUrl”不可分配给类型“字符串”
问题描述
我正在尝试清理 pdf url 并希望将其分配给字符串类型变量,以便我可以将其用于 pdf 查看器。有没有办法做到这一点?如果我将any
type 用于 pdfSrc 类型,我会得到Invalid parameter object: need either .data, .range or .url in the <pdf-viewer>
.
注意:我使用的 URL 仅供参考,我会在那个地方使用外部 URL
登陆页面.component.ts
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
export class LandingpageComponent implements OnInit {
public pdfSrc: string;
}
constructor(
private sanitizer: DomSanitizer) {
}
fnOpenAsset() {
let url = 'http://localhost/pdf_viewer-master/18/docs/pdf.pdf';
this.pdfSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
登陆页面.component.html
<pdf-viewer class="alignComponentCenter" [src]="pdfSrc"
</pdf-viewer>
解决方案
我有办法解决这个问题。sanitize()
我尝试使用返回值为字符串的方法再次清理 SafeResourceUrl | 无效的。
如果你想使用bypassSecurityTrustUrl()
,那么SecurityContext.URL
就会发生。在我的情况下,我使用SecurityContext.RESOURCE_URL
export class LandingpageComponent implements OnInit {
public pdfSrc: string;
}
constructor(
private sanitizer: DomSanitizer) {
}
fnOpenAsset() {
let url = 'http://localhost/pdf_viewer-master/18/docs/pdf.pdf';
this.pdfSrc = this.sanitizer.sanitize(SecurityContext.RESOURCE_URL, this.sanitizer.bypassSecurityTrustResourceUrl(url));
}```
推荐阅读
- jenkins-plugins - Jenkins - 通过 CIFS 插件错误发布
- javascript - REACT & Expressjs:后端不能“服务”静态前端
- java - 将参数化类型实例引用到其原始类型和使用原始类型引用另一个原始类型实例之间的区别?
- java - 循环错误时如何解决此问题?
- python-3.x - 无法按类名或 xpath selenium python 找到元素
- javascript - 烧瓶传递 json 和 render_template
- web-applications - 如果安装了使用 facebook 应用程序的动态链接
- mysql - 如何在 MySQL 中拆分文本和数字?
- java - Android Studio - 帮助我理解这段代码
- excel - 组合单独的日期和时间单元格以获取 hh:mm 格式的差异 (Excel)