Defining constants in typescript (ionic application)

In my Ionic app, I define constants as

//constants.ts
export var CONSTANTS = {
 API_ENDPOINT: 'http://localhost:3000/'
};

and import it as

import {CONSTANTS} from '../../services/constants'; //the path is correct

However, I get a CONSTANTS error not defined in the file where I import .. what am I missing here?

+4
source share
3 answers

Here's how you should do it:

// constants.ts
export const API_ENDPOINT= 'http://localhost:3000/';

And import it as:

import * as Constants from '../../services/constants';

And you can access it as follows:

Constants.API_ENDPOINT;
+10
source

For Ionic

app.value('config', {
  "constant1": "value1",
  "constant2": "value2"
});

and access it using

config.constant1

Remember to add a dependency config.

For nativescript

Identify

var configObject = {
    testData: false,
    apiUrl: "https://www.domain.com/api/v1/"
};

Using

var config = require('../../utils/config');

and get the value

config.apiUrl

Hi

+1
source

CONSTANTS, ,

. :

  • tsconfig.json: module.
  • console.log(CONSTANTS) in both files to see what happens
0
source

All Articles