管道' safeResourceUrl'无法找到

时间:2017-03-08 17:16:16

标签: javascript angular typescript

努力解决一些安全错误,因为我试图动态地将YouTube视频嵌入我的Angular 2应用中。在这里找到关于使用管道来清理网址的答案。

但遇到此当前错误。

  

管道' safeResourceUrl'无法找到

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

中导入

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>

1 个答案:

答案 0 :(得分:5)

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

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener


mapView = (MapView)findViewById(R.id.map);


    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(37.7750, 122.4183))
                    .title("San Francisco")
                    .snippet("Population: 776733"));
        }
    });
@NgModule({
    declarations: [
        SafeResourceUrl
    ],
    imports: [
      CommonModule
    ]
})
class SharedModule {