【发布时间】:2017-07-29 09:32:29
【问题描述】:
由于我试图将 youtube 视频动态嵌入到我的 Angular 2 应用程序中,因此正在努力解决一些安全错误。在此处找到有关使用管道清理 url 的答案。
但是遇到了当前的错误。
找不到管道“safeResourceUrl”
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
import { SafeResourceUrl } from '../shared/pipes/saferesourceurl.pipe';
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>
【问题讨论】:
标签: javascript angular typescript