gpt4 book ai didi

angularjs - DomSanitizationService 不是函数

转载 作者:太空狗 更新时间:2023-10-29 17:24:26 24 4
gpt4 key购买 nike

正如人们常说的,没有问题叫做愚蠢的问题。

我现在正在按照官方教程学习Angular 2.0。正如我从 packageconfig 文件中看到的那样,它使用的是 rc2.0。我试图抑制在 iFrame 标记中提示“不安全”url 的框架工作。

我已查看此 Stack Overflow Question 中的说明并关注 LIVE Plunker 中显示的所有内容。

我的 VS Code 在编译时没有报错,但在 Chrome 检查器中我可以看到错误。

enter image description here

以下是我的项目的TS文件。

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ParcelsService } from './parcels.service';
import { SafeResourceUrl, DomSanitizationService } from '@angular/platform-browser';
import { Parcel } from './mock-parcels';
@Component({
template: `
<h2>Parcels</h2>
<div *ngIf="parcel">
<h3>"{{parcel.checkUrl}}"</h3>
<iframe width=800 height=500 src="{{safeUrl}}}"></iframe>

<p>
<button (click)="gotoHeroes()">Back</button>
</p>
</div>
`,
providers:[ParcelsService, DomSanitizationService]
})
export class HeroDetailComponent implements OnInit, OnDestroy {
parcel: Parcel;
safeUrl : SafeResourceUrl;
private sub: any;
constructor(
private route: ActivatedRoute,
private router: Router,
private service: ParcelsService,
private sanitizer: DomSanitizationService) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id']; // (+) converts string 'id' to a number
this.parcel = this.service.getParcel(id);
});
this.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.parcel.checkUrl);

}
ngOnDestroy() {
this.sub.unsubscribe();
}
gotoHeroes() { this.router.navigate(['/heroes']); }
}

有没有人遇到过类似的问题?请帮助找出我做错了什么。

谢谢

最佳答案

您必须导入并提供 BROWSER_SANITIZATION_PROVIDERS:

import { BROWSER_SANITIZATION_PROVIDERS, 
SafeResourceUrl,
DomSanitizationService } from '@angular/platform-browser';

在你的 providers 数组中:

providers: [ParcelsService, BROWSER_SANITIZATION_PROVIDERS]

UPDATE: for the final release things changed a little

import { __platform_browser_private__, 
SafeResourceUrl,
DomSanitizer } from '@angular/platform-browser';

并将其添加到提供程序中,如下所示:

providers: [ParcelsService, __platform_browser_private__, BROWSER_SANITIZATION_PROVIDERS]

UPDATE FOR ANGULAR 4+: since Angular 4, there's some changes:

现在,您不必将 DomSanitizer 传递给 providers。您可以直接在您的组件中导入并在组件的 constructor 中获取它。 SafeResourceUrl 也是如此。

还有:

  • __platform_browser_private__ 不再位于 @angular/platform-b​​rowser 中。
  • BROWSER_SANITIZATION_PROVIDERS 不再位于 @angular/platform-b​​rowser 中。它现在 [作为提供者] 实现到 BrowserModule 中,可以从 @angular/platform-b​​rowser 导入。
  • 附言BrowserModule 通常添加到您的 app.module模块的导入数组。

关于angularjs - DomSanitizationService 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38353495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com