2
votes

I'm using Ionic 2 to develop a mobile application. The following are the information of my ionic installation:

  • Cordova CLI: 6.3.1
  • Ionic Framework Version: 2.0.0-rc.4
  • Ionic CLI Version: 2.1.18
  • Ionic App Lib Version: 2.1.9
  • Ionic App Scripts Version: 0.0.47
  • ios-deploy version: Not installed
  • ios-sim version: Not installed
  • OS: Windows 10
  • Node Version: v6.9.2
  • Xcode version: Not installed

I have an extension.ts file that contains various extensions of predefined types of TypeScript/JavaScript, including the following class:

export {};

declare global {
    interface Navigator {
        currentLang() : string;
    }
}

Navigator.prototype.currentLang = function() {
    var userLang = navigator.language.split('-')[0];
    userLang = /(en|it)/gi.test(userLang) ? userLang : 'en';
    return userLang;
};

When I run the command ionic serve the google developer console shows the following error:

TypeError: navigator.currentLang is not a function

What could be the problem? Am I forgetting something?

1
Can you show the part of your code that uses this function? How are you importing extension.ts? - Nitzan Tomer
I'm using navigator.currentLang in app.componet.ts inside the platform.ready().then(...) callback. I'm importing the extensions at the top of app.componet.ts using the line import { } from '../extensions'; - Balthier

1 Answers

0
votes

The compiled output of this code:

import { } from '../extensions';
console.log(navigator.currentLang());

Assuming the default module option is:

console.log(navigator.currentLang());

There's no import in the js file so when you try to run it in the browser it won't load ../extensions.js and therefor navigator.currentLang is undefined.
This is because you haven't actually imported anything from the module.

The syntax for that is:

import '../extensions';
console.log(navigator.currentLang());

Which compiles to:

require("../extensions");
console.log(navigator.currentLang());