gpt4 book ai didi

javascript - 找不到管道 'safeResourceUrl'

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:58 25 4
gpt4 key购买 nike

我正在努力解决一些安全错误,因为我正在尝试将 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

关于javascript - 找不到管道 'safeResourceUrl',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com