微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-找不到管道’safeResourceUrl’

由于我试图将youtube视频动态地嵌入到Angular 2应用中,因此正在努力解决一些安全性错误.在此处找到有关使用管道清理URL的答案.

但是遇到当前错误.

The pipe ‘safeResourceUrl’ Could not be found

enter image description here

SafeResourceUrl.pipe.ts

import { Pipe } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({name: 'safeResourceUrl'})
export class SafeResourceUrl {
    constructor(private sanitizer:DomSanitizer){}

    transform(url) {
        return this.sanitizer.bypassSecurityTrustResourceUrl(url);
    }
}

我将其导入到我的app.module中

import { NgModule } from '@angular/core';
import { HomeModule } from './home/home.module';
import { SharedModule } from './shared/shared.module';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SafeResourceUrl } from './shared/pipes/saferesourceurl.pipe';

@NgModule({
    declarations: [
        AppComponent,
        SafeResourceUrl
    ],
    imports: [
        SharedModule,
        HomeModule,
        AppRoutingModule
    ]
})

并导入到我的home.component.ts中

从“ ../shared/pipes/saferesourceurl.pipe”导入{SafeResourceUrl};

home.component.html的标记

<div class="container col-lg-3 col-md-6 col-sm-12" *ngFor="let card of category.categorycards">
<div class="thumbnail">
    <a href="/wiki/entity" *ngIf="card.type == 'image'">
        <div class="image-wrap">
            <img [src]="card.graphic" class="img-responsive" alt="[card.title]" title="[card.title]">
        </div>
    </a>

    <a href="/wiki/category" *ngIf="card.type == 'video'">
        <div class="image-wrap">
            <iframe title="YouTube video player"
                    class="youtube-player" type="text/html" 

                    [src]="card.url | safeResourceUrl"

                    height="100%" width="100%" frameborder="0"></iframe>
        </div>
    </a>

解决方法:

管道无法在全球范围内使用.无论它们在哪里使用,都需要将它们导入,与组件或指令相同.

@NgModule({
    declarations: [
        SafeResourceUrl
    ],
    imports: [
      CommonModule
    ]
})
class SharedModule {
@NgModule({
    declarations: [
      CommonModule,
      HomeComponent,
    ],
    imports: [
        SharedModule,
    ]
})
class HomeModule

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐