正确的方法使用Angular 2 RC6为Component提供DomSanitizer

kal*_*mas 36 javascript typescript angular-services angular angular-dom-sanitizer

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

我正在使用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() {}
}
Run Code Online (Sandbox Code Playgroud)

这会导致this.sanitizer.bypassSecurityTrustResourceUrl is not a function运行时出错.

有人能告诉我一个如何正确提供DomSanitizer提供商的例子吗?谢谢!

mic*_*yks 58

你不需要再申报providers: [ DomSanitizer ].
只需要如下图所示,import DomSanitizer

import { DomSanitizer, SafeResourceUrl, SafeUrl} from '@angular/platform-browser';
Run Code Online (Sandbox Code Playgroud)

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

constructor(private sanitizer: DomSanitizer) {}
Run Code Online (Sandbox Code Playgroud)

  • 好的,我确实尝试过。我不确定是否因为子模块实际上是我使用 ng-packagr 构建的库的一部分。我在我的应用程序项目中使用这个库。但是,我仍然收到一条错误消息,指出没有为 DomSanitizer 提供提供程序。我最终只是做了一个本机 JavaScript 实现来替代我试图从 DomSanitizer 使用的操作。我仍在学习如何创建 angular 打包库,所以我不确定这是否会影响这里的导入。你有开发图书馆的经验吗? (2认同)

San*_*ket 11

进口这些 -

import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
Run Code Online (Sandbox Code Playgroud)

定义变量 -

trustedDashboardUrl : SafeUrl;
Run Code Online (Sandbox Code Playgroud)

在像这样的构造函数中使用它 -

constructor(
    private sanitizer: DomSanitizer) {}
Run Code Online (Sandbox Code Playgroud)

像这样的特定网址 -

this.trustedDashboardUrl =
                        this.sanitizer.bypassSecurityTrustResourceUrl
                            ("URL");
Run Code Online (Sandbox Code Playgroud)

看看这是否有帮助.

  • *不要*简单地规避 DOM 清理!您将使您的页面容易受到 XSS 攻击! (2认同)

小智 7

如果您可以为SanitizedHtmlPipe添加自定义管道,那就更容易了。因为我们可以在角度项目中全局使用:

  • 清理-html.pipe.ts

    import { Pipe, PipeTransform } from '@angular/core';
    import { DomSanitizer } from '@angular/platform-browser'
    @Pipe({
      name: 'sanitizedHtml'
    })
    export class SanitizedHtmlPipe implements PipeTransform {
      constructor(private sanitized: DomSanitizer) {}
      transform(value: any): any {
        return this.sanitized.bypassSecurityTrustHtml(value);
      }
    }

Run Code Online (Sandbox Code Playgroud)
  • 英雄.组件.html

    <div [innerHTML]="htmlString | sanitizedHtml"></div>

Run Code Online (Sandbox Code Playgroud)
  • 英雄组件.ts

    import { Component, OnInit } from '@angular/core';
    @Component({
      selector: 'app-hero',
      templateUrl: './hero.component.html',
      styleUrls: ['./hero.component.css']
    })
    export class HeroComponent implements OnInit {
      htmlString: any;
      constructor() { }
      ngOnInit(): void {
        this.htmlString = `
        <div class="container">
          <header class="blog-header py-3">
            <div class="row flex-nowrap justify-content-between align-items-center">
              <div class="col-4 pt-1">
                <a class="text-muted" href="#">Subscribe</a>
              </div>
              <div class="col-4 text-center">
                <a class="blog-header-logo text-dark" href="#">Large</a>
              </div>
              <div class="col-4 d-flex justify-content-end align-items-center">
                <a class="text-muted" href="#">
                  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="mx-3"><circle cx="10.5" cy="10.5" r="7.5"></circle><line x1="21" y1="21" x2="15.8" y2="15.8"></line></svg>
                </a>
                <a class="btn btn-sm btn-outline-secondary" href="#">Sign up</a>
              </div>
            </div>
          </header>
        </div>`;
      }
    }

Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,您可以访问此链接

  • 有人知道如何使用 sanatize 方法而不是绕过证券吗??? (6认同)