由于我试图将youtube视频动态地嵌入到Angular 2应用中,因此正在努力解决一些安全性错误。在此处找到有关使用管道清理URL的答案。
但是遇到当前错误。
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>
最佳答案
管道无法在全局范围内使用。无论它们在哪里使用,都需要将它们导入,与组件或指令相同。
@NgModule({
declarations: [
SafeResourceUrl
],
imports: [
CommonModule
]
})
class SharedModule {
@NgModule({
declarations: [
CommonModule,
HomeComponent,
],
imports: [
SharedModule,
]
})
class HomeModule
关于javascript - 找不到管道 'safeResourceUrl',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42677714/