【问题标题】:The pipe 'safeResourceUrl' could not be found找不到管道“safeResourceUrl”
【发布时间】: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


    【解决方案1】:

    管道无法在全球范围内使用。它们需要在任何使用它们的地方导入,与组件或指令相同。

    @NgModule({
        declarations: [
            SafeResourceUrl
        ],
        imports: [
          CommonModule
        ]
    })
    class SharedModule {
    
    @NgModule({
        declarations: [
          CommonModule,
          HomeComponent,
        ],
        imports: [
            SharedModule,
        ]
    })
    class HomeModule
    

    【讨论】:

    • 啊,明白了,一旦我将它导入我的SharedModule :) 并且从app.module 中删除,也不会再出现错误。谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2016-12-24
    • 2017-09-04
    • 2017-11-29
    • 2017-11-10
    相关资源
    最近更新 更多