在使用深度链接和延迟加载 Ionic 3 时,我们如何在管道文件中使用 Provider? [英] How can we use Provider in Pipe files while using Deep Linking and Lazy Loading Ionic 3?

查看:15
本文介绍了在使用深度链接和延迟加载 Ionic 3 时,我们如何在管道文件中使用 Provider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个名为 TranslatePipe.ts 的管道中使用一个名为 Translation.ts 的 Provider 和一个 returnResult 函数.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,我使用延迟加载.我的页面是 ExamplePage在 ExamplePage.html 我想使用管道作为 {{'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
  ]
})

有一些与这个主题相关的东西,但它们不起作用,例如

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

我尝试修改 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.tsexample.module 中包含 provider 和 pipe.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
  ]

这篇关于在使用深度链接和延迟加载 Ionic 3 时,我们如何在管道文件中使用 Provider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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