在使用Deep Linking和Lazy Loading Ionic 3时,我们如何在Pipe文件中使用Provider? [英] How can we use Provider in Pipe files while using Deep Linking and Lazy Loading Ionic 3?

查看:132
本文介绍了在使用Deep Linking和Lazy Loading Ionic 3时,我们如何在Pipe文件中使用Provider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在名为 TranslatePipe.ts Translation.ts 的提供程序,其中包含returnResult函数$ C>。 Translation.ts是

I want to use a Provider, named as Translation.ts with a returnResult function in a Pipe named as TranslatePipe.ts. Translation.ts is

@Injectable()
export class TranslationProvider

  constructor() { }

  getResult(value) { returns something ; }

}

和TranslatePipe.ts为

and TranslatePipe.ts is given as

@Pipe({
  name: 'translation',
})
export class TranslationPipe implements PipeTransform {

  constructor(public translateService: TranslationProvider) {

  }


  transform(value: string, ...args) {
    return this.translateService.getResult(value);
  }

我的项目是Ionic 3,我使用Lazy Loading。我的页面是ExamplePage
在ExamplePage.html中我想使用piping作为{{'something'|翻译}}

MY project is Ionic 3 and I use Lazy Loading. My page is ExamplePage In ExamplePage.html I want to use piping as {{'something' | translation}}

和example.ts是

and example.ts is

@IonicPage()
@Component({
  selector: 'example',
  templateUrl: 'example.html',
})
export class ExamplePage {

  constructor(public translate: TranslationProvider) {  }
}

app.module是类似的东西

app.module is something like that

@NgModule({
  declarations: [
    TranslatePipe,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [ .. ],
  providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    TranslationProvider
  ]
})

这个主题有些相关但是他们不这样做as

There is something related with this subject but they dont work such as

  • Ionic 3 Lazy Loading with amDateFormat pipe or
  • Pipe not found in custom component - Ionic 3 (Lazy Loading)

我尝试修改example.module,我添加了Provider和Pipe但它没有用。
你能否给我一些关于解决方案的建议?

I try to modify example.module, I added Provider and Pipe but it did not work. Would you give me any recommendation about the solution?

推荐答案

包括提供者和管道 app.module.ts example.module.ts 中,根据您是要在更多页面中使用还是仅在示例页面中使用,而不是两者都使用。

Include both provider and pipe in either app.module.ts OR example.module.ts based on whether you want to use in more pages or only in example page and not both.

您需要将它们添加到同一模块中。

You need to add them in the same module.

 declarations: [
    TranslatePipe,
  ],
  //...
  providers: [
    TranslationProvider
  ]

这篇关于在使用Deep Linking和Lazy Loading Ionic 3时,我们如何在Pipe文件中使用Provider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆