正确的方法使用Angular 2 RC6为Component提供DomSanitizer [英] Correct way Provide DomSanitizer to Component with Angular 2 RC6

查看:280
本文介绍了正确的方法使用Angular 2 RC6为Component提供DomSanitizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DomSanitizer来清理组件中的动态URL,我似乎无法弄清楚为此服务指定提供商的正确方法是什么。

I'm attempting to use DomSanitizer to sanitize a dynamic URL within a Component using I can't seem to figure out what the correct way to specify a Provider for this service is.

我正在使用 Angular 2.0.0-rc .6

这是我当前的组件:

@Component({
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ],
    providers: [ DomSanitizer ],
})
export class AppComponent implements OnInit
{
    public url: SafeResourceUrl;

    constructor(private sanitizer: DomSanitizer) {}

    ngOnInit() {
        let id = 'an-id-goes-here';
        let url = `https://www.youtube.com/embed/${id}`;

         this.videoUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
    }

    ngOnDestroy() {}
}

这导致错误 this.sanitizer.bypassSecurityTrustResourceUrl在运行时不是函数

有人可能会显示我是如何为DomSanitizer正确提供Provider的一个例子?谢谢!

Could someone show me an example of how to properly provide a Provider for DomSanitizer? Thanks!

推荐答案

您无需声明 提供者:[DomSanitizer ]

只需要 导入 DomSanitizer ,如下所示,

You don't need to declare providers: [ DomSanitizer ] anymore.
Just need to import DomSanitizer as shown below,

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

在组件中通过构造函数注入它,如下所示,

in component inject it through a constructor as below,

constructor(private sanitizer: DomSanitizer) {}

这篇关于正确的方法使用Angular 2 RC6为Component提供DomSanitizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆