Search code examples
javascriptangularecmascript-6ionic2ng2-translate

Angular2 - TypeScript to JavaScript ES6 Syntax - ng2-translate


I am trying to implement ng2-translate pipe on my Angular2/Ionic2 app which is totally written on JavaScript ES6.

However, I am stuck on a step in the setup phase in which I need to write a piece of code in JavaScript ES6. What I found so far on their documentation and over the internet is written on TypeScript which throws an Syntax error while trying to implement it on my app.

The code:

      @Component({
          template: '<ion-nav swipeBackEnabled="true" [root]="rootPage"></ion-nav>',
        config: {}, 
        providers: [
         {
         //The Syntax error throws on this line, which is needed to be re-written on JS
           provide: TranslateLoader,
           useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
           deps: [Http]
         },
         TranslateService
       ],
       pipes: [TranslatePipe]
      })

What I need to know is that how to convert that piece of code written on TypeScript to JavaScript ES6 in order to make it run.


Solution

  • Without knowing what the error is... Perhaps it is sufficient to remove the type from your function definition for the attribute useFactory.

      @Component({
          template: '<ion-nav swipeBackEnabled="true" [root]="rootPage"></ion-nav>',
        config: {}, 
        providers: [
         {
           provide: TranslateLoader,
           useFactory: (http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
           deps: [Http]
         },
         TranslateService
       ],
       pipes: [TranslatePipe]
      })