微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在带有 Angular 的打字稿中使用翻译管道

如何解决如何在带有 Angular 的打字稿中使用翻译管道

我想在单击按钮时显示已翻译的消息,因此我需要在打字稿中使用翻译管道。

这是我的json:

 "newRepair": {
   "intake": {
       "errormessage" : "This is a required field"
      }
     }

这是TS中按钮相关的功能

 error : string;
 saveAndGoForUpload(){
   this.error = this.translatePipe.transform("newRepair.intake.errormessage");}

HTML:

<button  (click)="saveAndGoForUpload()" type="button"
     class="btn">PROCEED</button>
 <p>{{error}}</p>

它给了我这个错误NullInjectorError: No provider for TranslatePipe!

解决方法

如果你想在组件中使用管道的transform()方法,你还需要将CustomPipe添加到模块的提供者中:

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

@NgModule({
  imports:      [ ... ],declarations: [ AppComponent,CustomPipe ],providers: [CustomPipe],bootstrap:    [ AppComponent ]
})
export class AppModule { }

,

您需要在类组件装饰器中添加自定义管道作为提供者:

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

@Component({
    templateUrl: './some.component.html',providers: [CustomPipe]
})
export class SomeComponent{
}

或者在模块级别,取决于您计划在何处使用管道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。