【问题标题】:Custom pipe not found ionic 4 | pipe not found未找到自定义管道 ionic 4 |找不到管道
【发布时间】:2023-04-09 22:01:01
【问题描述】:

大家好,我是新的 ionic 程序员,我目前正在开发一个使用 youtube api 的应用程序(显示来自单个频道的视频)。我遵循了一个教程,我和他做同样的事情,但我的得到了这个错误。

The pipe 'youtube' could not be found 

即使我按照教程所示创建并导入了 youtube 管道。 这些是我的代码

这是我的 app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import {HttpModule} from '@angular/http';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import {YoutubePipe} from './youtube.pipe';



@NgModule({
declarations:
  [AppComponent, YoutubePipe],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpModule],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}

这是我的 youtube.pipe.ts

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

@Pipe({
name: 'youtube',
})
export class YoutubePipe implements PipeTransform {

constructor(private dom: DomSanitizer) {

}

transform(value: string) {
console.log(this.dom.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/' + value ));
return this.dom.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/' + value);
}
}

这个是我的home.page.html

<ion-header>
<ion-toolbar>
<ion-title>
  Videos
</ion-title>
</ion-toolbar>
</ion-header>

<ion-content padding>
<ion-card *ngFor="let item of posts">
<ion-card-title>
{{item.snippet.title}}
</ion-card-title>
<ion-card-content>
  <!--img [src]="item.snippet.thumbnails.high.url"/>-->
  <iframe [src]="item.id.videoId | youtube" width="100%" height="315"  frameborder="0" allowfullscreen></iframe>
</ion-card-content>
</ion-card>
</ion-content>

最后一个是我的home.page.ts

import { Component } from '@angular/core';
import {Http} from '@angular/http';

import {NavController} from '@ionic/angular';
import {map} from 'rxjs/operators';



@Component({
selector: 'app-home',
templateUrl: './home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {

search: String = 'ionic 4';
posts: any = [];

constructor(public navCtrl: NavController, public http: Http) {
const url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId=UCHm8H-_IZMLl4-HYwrsvtNQ&maxResults=20&key=AIzaSyCQuzbBfetLjteTBAoSV3oCM3Mf_dstU6Q';
this.http.get(url).pipe(map(res => res.json())).subscribe(data => {
  this.posts = this.posts.concat(data.items);
  console.log(this.posts);
});

}

}

希望你们能帮助我。 谢谢

【问题讨论】:

标签: angular typescript ionic-framework ionic4


【解决方案1】:

我有以下解决方案,在 Ionic 4 的组件中使用切割管道。 从 app.module.ts 中移除管道

在您的页面模块中导入管道。例如home.module.ts

import { CustomPipe } from '../../app/custom.pipe';

将此添加到 home.module.ts 中的声明

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  entryComponents: [NewssliderComponent],
  declarations: [HomePage, CustomPipe, NewssliderComponent]
})

你可以在 newsslider.component.html 文件中使用管道

{{e.publish | CustomPipe}}

【讨论】:

    【解决方案2】:

    不要将管道导入 app.module.ts。而是创建一个名为 pipe.module.ts 的文件,其中包含以下代码:

    import { NgModule } from '@angular/core';
    import { YoutubePipe } from './youtube.pipe';
    @NgModule({
        declarations: [
            YoutubePipe
        ],
        imports: [],
        exports: [
            YoutubePipe
        ]
    })
    export class PipesModule {}
    

    然后将此 PipesModule 导入到您正在使用管道的 home.module.ts 中。还要确保从 app.module.ts 中删除 import { YoutubePipe } from './youtube.pipe',因为不需要在 app.module.ts 中导入。

    【讨论】:

    • 这应该被接受为答案,因为它有助于在 ionic 4 的多个页面上导入相同的管道
    猜你喜欢
    • 2019-05-31
    • 2016-12-24
    • 1970-01-01
    • 2017-12-04
    • 2018-06-13
    • 1970-01-01
    • 2020-12-04
    • 2017-09-04
    • 2017-03-20
    相关资源
    最近更新 更多