由于我正在尝试将 YouTube 视频动态嵌入到我的 Angular 2 应用程序中,因此正在努力解决一些安全错误。在这里找到了关于使用 Pipe 来清理 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>
管道无法在全球范围内提供。无论在哪里使用它们,都需要导入它们,就像组件或指令一样。
@NgModule({
declarations: [
SafeResourceUrl
],
imports: [
CommonModule
]
})
class SharedModule {
@NgModule({
declarations: [
CommonModule,
HomeComponent,
],
imports: [
SharedModule,
]
})
class HomeModule
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)