gpt4 book ai didi

javascript - 找不到管道 : Angular 5 custom pipe

转载 作者:行者123 更新时间:2023-12-01 15:51:42 25 4
gpt4 key购买 nike

我已阅读 this postthis article .我相信我已经完成了建议的一切:将管道添加到共享的模块中。

但是,无论我做什么,我都无法让我的模板找到我创建的管道。我的应用程序已经有一个其他模块导入的共享模块,因此我创建了管道并将其添加到共享模块中:

我用 ng g pipe /shared/pipes/safe --flat --module shared --spec=false 创建它

在 SharedModule 中,我也将它添加到 declarationsproviders .

一切都在运行,但我尝试使用管道,如下所示:
<iframe width="600" height="360" [src]="video.acf.youtube_url | safe: 'url'" frameborder="0" allowfullscreen></iframe>
我只是得到一个错误
Error: Uncaught (in promise): Error: Template parse errors: The pipe 'safe' could not be found
管道本身是

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
name: 'safe'
})
export class SafePipe implements PipeTransform {

constructor(protected sanitizer: DomSanitizer) { }

public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
switch (type) {
case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
default: throw new Error(`Invalid safe type specified: ${type}`);
}
}
}

最佳答案

您需要在 下添加它SharedModule 还有导出,

exports: [
SafePipe
]

关于javascript - 找不到管道 : Angular 5 custom pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450717/

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